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 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
|2000H||START||LXI H, 2040H||Load size of array|
|2003H||MVI D, 00H||Clear D register to set up a flag|
|2005H||MOV C, M||Set C register with number of elements in list|
|2006H||DCR C||Decrement C|
|2007H||INX H||Increment memory to access list|
|2008H||CHECK||MOV A, M||Retrieve list element in Accumulator|
|2009H||INX H||Increment memory to access next element|
|200AH||CMP M||Compare Accumulator with next element|
|200BH||JC NEXTBYTE||If accumulator is less then jump to NEXTBYTE|
|200EH||MOV B, M||Swap the two elements|
|200FH||MOV M, A|
|2011H||MOV M, B|
|2013H||MVI D, 01H||If exchange occurs save 01 in D register|
|2015H||NEXTBYTE||DCR C||Decrement C for next iteration|
|2016H||JNZ CHECK||Jump to CHECK if C>0|
|2019H||MOV A, D||Transfer contents of D to Accumulator|
|201AH||CPI 01H||Compare accumulator contents with 01H|
|201CH||JZ START||Jump to START if D=01H|
- Retrive 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 8085 program to add two 16 bit numbers
- 8085 program to add 2-BCD numbers
- 8085 program to add two 8 bit numbers
- 8085 program to swap two 8-bit numbers
- 8085 program to reverse 16 bit number
- 8085 program to multiply two 16-bit numbers
- 8085 program to sum of two 8 bit numbers without carry
- 8085 program to find the sum of a series
- 8085 program to check whether the given number is even or odd
- 8085 program to reverse 8 bit number
- 8085 program to divide two 8 bit numbers
- 8085 program to add numbers in an array
- 8085 program for pulse waveform
- 8085 program for Binary search
- 8085 program for hexadecimal counter
- 8085 program for Linear search | Set 2
- 8085 program to multiply two 8 bit numbers
- 8085 program to find the set bit of accumulator
- 8085 program to divide two 16 bit numbers
- 8085 program to convert gray to binary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.