Bubble Sort (Bubble Sort)
What is Bubble Sort?
Bubble arrangement is a simple sort algorithm. This sorting algorithm is carried out based on comparing pairs of adjacent and swap elements if they are not in order.
This algorithm is not suitable for use with large data sets where the worst case complexity and the average case are Ο (n2) where n is the number of elements.
Foam sorting algorithm is the slowest algorithm among basic sorting algorithms. This algorithm is even slower than the direct displacement algorithm even though the number of comparisons is equal, but due to the displacement of two adjacent elements, the number of swapped places is more.
How to arrange foam to work?
Suppose we have an unordered array of elements like below. Now we use the bubble sort algorithm to sort this array.
The bubble sort algorithm starts with the first two elements, comparing them to check which element is larger.
In this case, 33 is greater than 14, so these two elements were in order. Next we compare 33 and 27.
We see that 33 is greater than 27, so these two values need to be swapped in order.
New arrays will be as follows:
Next we compare 33 and 35. These two values are in order.
Then we compare the next two values of 35 and 10.
Since 10 is less than 35, these two values are not in order.
Swap the order of two values. We have reached the end of the array. So after a loop, the array will look like this:
For simplicity, I will display the image of the array after each loop. After the second iteration, the array will look like:
After each loop, at least one value will move to the last position. After the third loop, the array will look like:
And when there is no need to swap any element order, the bubble sort algorithm finds that the array has been sorted.
Next, we learn some more practical aspects of sorting algorithms.
Solution for bubble sort (Bubble Sort)
Suppose the list is an array with n elements. Then assume the swap function to swap the values of the elements in the array (this is assuming, of course, you can write your own code for this swap function).
B ắ t đầ u gi ả i thu ậ t BubbleSort ( list ) for t ấ t c ả ph ầ n t ử trong list if list [ i ] > list [ i + 1 ] swap ( list [ i ], list [ i + 1 ]) k ế t th ú c if k ế t th ú c for return list K ế t th ú c BubbleSort
Sample algorithm for bubble sort (Bubble Sort)
We see that the bubble sort algorithm compares each pair of elements in the array unless the entire array is completely sorted in ascending order. This can increase complexity, ie increase unnecessary comparison and swap operations if this array does not need any swap when all the elements are sorted in ascending order. already.
To avoid this happening, we can use a swapped variable for example to help us know whether to perform a swap operation. If not necessary, exit the loop.
You follow the following sample illustration algorithm:
B ắ t đầ u h à m bubbleSort ( list : m ả ng c á c ph ầ n t ử ) loop = list . count ; for i = 0 t ớ i loop - 1 th ự c hi ệ n : swapped = false for j = 0 t ớ i loop - 1 th ự c hi ệ n : /* so sánh các phần tử cạnh nhau */ if list [ j ] > list [ j + 1 ] then /* tráo đổi chúng */ swap ( list [ j ], list [ j + 1 ] ) swapped = true k ế t th ú c if k ế t th ú c for /*Nếu không cần tráo đổi phần tử nào nữa thì tức là mảng đã được sắp xếp. Thoát khỏi vòng lặp.*/ if ( not swapped ) then break k ế t th ú c if k ế t th ú c for K ế t th ú c h à m return list
Deploy the algorithm of arranging foam in C
One more thing that we have not mentioned in the two parts of algorithm design is that after each loop, the largest values will appear at the end of the array (as shown in the illustration: after the 1st loop is 35 ; after loop 2 is 33 and 35; .). Therefore, the next loop will not need to include these sorted elements. To do this, in the code we limit the loop iteration side to avoid having to repeat these sorted values.
According to Tutorialspoint
Previous lesson: Algorithms arranged in data structures & algorithms
Next lesson: Insertion sort algorithm (Insertion Sort)
You should read it
- How to Clean Vive Foam
- Should a foam bottle be used for a car wash?
- 5 Reasons to use foam mats for babies
- Why do so many people addicted to squeeze foam?
- How to Replace RV Ceiling Fabric
- Decode the mystery of the interesting phenomenon that slammed into the mouth of a new beer bottle spitting foam
- The computer does not need a fan to dissipate heat thanks to the copper foam heat sink
- Algorithms arranged in data structures & algorithms
May be interested
- Sort information on Facebookin the past, users could lose posts when they ignored the stream, especially if you gave facebook a large amount of time to access every day.
- Collection of Bubble Images PNGthe image of bubbles floating in the air makes us feel like we are back to childhood with blowing bubbles of soap. in the article below, tipsmake.com send you readers beautiful bubble images with png format for stitching, image editing.
- Sort names alphabetically in Wordyou are asked by your boss to organize the list of employees alphabetically and not sure what to do? this article will show you how to sort names by abc
- The mysterious bubble that appears in the center of the Milky Way galaxy is finally 'decoded'.since its first discovery to date, the fermi bubble has caused astronomers around the world to have a 'headache' for not being able to find the origin of this giant gamma-ray structure.
- Sort data in Excelsort data in excel - excel supports you with data sorting tools to give you the sorting options. you can use the sort tool to sort the data in your excel document.
- How to sort data in Excel using Sort is extremely simplewith just a few operations to arrange data in excel, you can transform your spreadsheet to become neat and easy to see. let's find out with free download now!
- How to sort by multiple columns in Google Sheetswhen you are analyzing complex data sets in google sheets, you can take advantage of the built-in sorting feature to organize your data. you can sort in columns or for more complex data, you can sort in multiple columns.
- MS Excel 2007 - Lesson 8: Sort and Filtersort and filter are features that allow you to manipulate data in a spreadsheet based on standards. to sort data and filter data in excel 2007 you will use the sort & filter feature. below tipsmake.com will guide you how to filter data, arrange data with this feature in excel 2007.
- Excel does not have Sort Oldest to Newest, what should I do?you are sorting time data but excel does not have sort oldest to newest. fix the display error problem with tipsmake now.
- How to sort dates in ascending and descending ways in Excelhow to sort dates in ascending and descending ways in excel. the following article helps you to sort dates in ascending or descending order in excel quickly and effectively. method 1: step 1: select the data column containing dates to sort - on the data tab