Bubble sort is a simple sorting algorithm. It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted. The name of this algorithm comes from the fact that the smaller elements are slowly "floated" to the top of the sequence by swapping.
Example of bubble sort :
The algorithm of bubble sort is implemented as follows: [After sorting, the array is arranged from small to large]
/** * Bubble Sort * Compare adjacent elements. If the first is bigger than the second, swap the two of them. * Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number. * Repeat the above steps for all elements except the last one. * Continue to repeat the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare. * @param numbers integer array to be sorted */ public static void bubbleSort(int[] numbers) { int temp = 0; int size = numbers.length; for(int i = 0 ; i < size-1; i ++) { for(int j = 0 ;j < size-1-i ; j++) { if(numbers[j] > numbers[j+1]) //Swap two numbers { temp = numbers[j]; numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } } }
quicksort
The basic idea of quick sort :
Divide the records to be sorted into two independent parts by one sorting, and the keywords of one part of the records are smaller than that of the other part, then continue to sort the two parts separately until the whole sequence is in order.
Example of quick sort :
(a) A process of sorting:
(b) The whole process of sorting
Treat the entire sequence as an array, treat the zeroth position as the central axis, and compare it with the last one. If it is smaller than it, swap it, and do nothing if it is larger than it; Small is not exchanged, it is bigger than him. This cycle repeats, and the sorting is completed in one pass. The left side is smaller than the central axis, and the right side is larger than the central axis. Then use the divide and conquer method to sort these two independent arrays respectively.
The code is implemented as follows:
1. Find the position of the central axis (the lowest position as the central axis)
/** * Find out the position of the central axis (the default is the lowest position low) after sorting the numbers array * * @param numbers with lookup array * @param low start position * @param high end position * @return the position of the center axis */ public static int getMiddle(int[] numbers, int low,int high) { int temp = numbers[low]; //The first of the array is used as the central axis while(low < high) { while(low < high && numbers[high] > temp) { high--; } numbers[low] = numbers[high];//Records smaller than the central axis are moved to the low end while(low < high && numbers[low] < temp) { low++; } numbers[high] = numbers[low] ; //Records larger than the center axis are moved to the high end } numbers[low] = temp ; //The middle axis is recorded to the end return low ; // return the position of the center axis }
2. Divide and conquer sorting algorithm in recursive form:
/** * * @param numbers with sorted array * @param low start position * @param high end position */ public static void quickSort(int[] numbers,int low,int high) { if(low < high) { int middle = getMiddle(numbers,low,high); // Divide the numbers array into two quickSort(numbers, low, middle-1); //Recursively sort the low field table quickSort(numbers, middle+1, high); //Recursively sort the high field table } }
3. Quick sort provides method calls
/** * Quick sort * @param numbers with sorted array */ public static void quick(int[] numbers) { if(numbers.length > 0) //Check if the array is empty { quickSort(numbers, 0, numbers.length-1); } }
analyze:
Quicksort is generally considered to have the best average performance among sorting methods of the same order of magnitude (O(nlog2n)) . However, if the initial sequence is ordered or basically ordered by key, the quick sort degenerates into a bubble sort instead. In order to improve it, the benchmark record is usually selected by the method of "taking the middle of the three", that is, the two end points of the sorting interval and the three record key codes of the midpoint are adjusted as the pivot record. Quicksort is an unstable sorting method.
method test
print function:
public static void printArr(int[] numbers) { for(int i = 0 ; i < numbers.length ; i ++ ) { System.out.print(numbers[i] + ","); } System.out.println(""); }
test:
public static void main(String[] args) { int[] numbers = {10,20,15,0,6,7,2,1,-5,55}; System.out.print("Before sorting: "); printArr(numbers); bubbleSort(numbers); System.out.print("After bubble sort: "); printArr(numbers); quick(numbers); System.out.print("After quicksort: "); printArr(numbers); }
result:
Before sorting: 10,20,15,0,6,7,2,1,-5,55, After bubble sort: -5,0,1,2,6,7,10,15,20,55, After quicksort: -5,0,1,2,6,7,10,15,20,55,