Bubble sort
Bubble sort is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.
The algorithm for bubbling sorting is implemented as follows: "After sorting, the array is arranged from small to large"
/*** Bubble Sort * Compare adjacent elements. If the first one is bigger than the second one, swap them both. * For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number. * Repeat the above steps for all elements except the last one. * Continue to repeat the above steps each time for fewer elements until there are no pairs of numbers to compare. * @paramnumbers array of integers that need to be sorted*/ Public Static voidBubblesort (int[] numbers) { intTemp//Record interim intermediate values intsize = Numbers.length;//Array Size for(inti = 0; i < size-1; i++) { for(intj = i + 1; J < size; J + +) { if(Numbers[i] > Numbers[j])//Exchange two number of positions{Temp=Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; } } } }
Quick Sort
The basic idea of quick sorting :
Divide the records to be sorted into separate two parts by a single pass, where some of the recorded keywords are smaller than the other, and then the two sections continue to be sorted until the entire sequence is ordered.
The following diagram is a sorting process:
Think of the whole sequence as an array, the 0th position as the middle axis , and the last one, if it is smaller than it, does not do any processing than it; The end is exchanged again and the smaller one, than it is smaller than that, which does not exchange much larger than it. This cycle, a trip to the completion of the order, the left side is smaller than the middle axis, the right is larger than the middle axis, and then the division of the method, respectively, the two separate array to sort.
The code is implemented as follows:
1. Find the middle axis (the lowest bit as the middle axis) where
/*** Find the middle axis (the default is the lowest bit low) in the numbers array after the location of * *@paramnumbers with find array *@paramLow start position *@paramHigh End Position *@returnWhere is the middle axis*/ Public Static intGetmiddle (int[] numbers,intLowintHigh ) { inttemp = Numbers[low];//the first of the arrays as a middle axis while(Low <High ) { while(Low < High && Numbers[high] >temp) { High--; } Numbers[low]= Numbers[high];//Less than mid-axis records moved to the low end while(Low < High && Numbers[low] <temp) { Low++; } Numbers[high]= Numbers[low];//a record larger than the middle axis moves to the high end} Numbers[low]= temp;//Middle axis record to tail returnLow;//returns the position of the middle axis}
2, recursive form of sorting algorithm:
/** * * @paramnumbers with sorted array *@paramLow start position *@paramHigh End Position*/ Public Static voidQuickSort (int[] numbers,intLowintHigh ) { if(Low <High ) {intMiddle = getmiddle (Numbers,low,high);//divides a numbers array into a splitQuickSort (Numbers, low, middle-1);//recursive ordering of low-field tablesQuickSort (Numbers, middle+1, high);//recursively sort a high-field table } }
3. Quick Sort provides method invocation
/** * Quick sort @param numbers with sorted array */public Static void quick (int[] numbers) { if(Numbers.length > 0) // See if the array is empty { 0, numbers.length-1);} }
method Test
Print function:
Public Static void Printarr (int[] numbers) { for (int i = 0; i < Numbers.length; 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 quick sorting:"); Printarr (numbers); }
Results:
Sort before: 10,20,15,0,6,7,2,1,-5,55, bubble sort after:-5,0,1,2,6,7,10,15,20,55, after quick sorting:- 5,0,1,2,6,7,10,15,20,55,
Thanks: Thank you for your patience and reading!
Java Sorting algorithm (i) bubble sort, quick sort