8085 program for bubble sort
Prerequisite – Bubble Sort
Problem – Write an assembly language program in 8085 microprocessor to sort a given list of n numbers using Bubble Sort.
Assumption – Size of list is stored at 2040H and list of numbers from 2041H onwards.
- Load size of list in C register and set D register to be 0
- Decrement C as for n elements n-1 comparisons occur
- Load the starting element of the list in Accumulator
- Compare Accumulator and next element
- If accumulator is less than or equal to the next element jump to step 8
- Swap the two elements
- Set D register to 1
- Decrement C
- If C>0 take next element in Accumulator and go to point 4
- If D=0, this means in the iteration, no exchange takes place consequently we know that it won’t take place in further iterations so the loop in exited and program is stopped
- Jump to step 1 for further iterations
|LXI H, 2040H
|Load size of array
|MVI D, 00H
|Clear D register to set up a flag
|MOV C, M
|Set C register with number of elements in list
|Increment memory to access list
|MOV A, M
|Retrieve list element in Accumulator
|Increment memory to access next element
|Compare Accumulator with next element
|If accumulator is less then jump to NEXTBYTE
|If accumulator is equal then jump to NEXTBYTE
|MOV B, M
|Swap the two elements
|MOV M, A
|MOV M, B
|MVI D, 01H
|If exchange occurs save 01 in D register
|Decrement C for next iteration
|Jump to CHECK if C>0
|MOV A, D
|Transfer contents of D to Accumulator
|Compare accumulator contents with 01H
|Jump to START if D=01H
- Retrieve an element in accumulator.
- Compare it with next element, if it is greater then swap otherwise move to next index.
- If in one entire loop there has been no exchange, halt otherwise start the whole iteration again.
- The following approach has two loops, one nested inside other so-
Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.
Best Case Time Complexity: O(n). Best case occurs when array is already sorted.
Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each adjacent pair of elements, and swaps them if they are in the wrong order. The algorithm gets its name from the way smaller elements “bubble” to the top of the list. Here’s an 8085 assembly language program for bubble sort
Advantages of bubble sort:
- Bubble sort is easy to understand and implement, even in assembly language.
- Bubble sort is efficient for small lists and nearly sorted lists.
- Bubble sort has a simple logic that is easy to debug and modify.
Disadvantages of bubble sort:
- bubble sort has a worst-case and average-case time complexity of O(n^2), which makes it inefficient for large lists.
- Bubble sort takes more time to execute compared to other sorting algorithms like Quick sort and Merge sort.
- Bubble sort does not work efficiently for lists with random or unsorted elements.
Share your thoughts in the comments
Please Login to comment...