bubble sort (algorithm to ask for interview)First, the basic idea: each comparison of adjacent two elements, on-demand adjustment of the order Second, the title: Request 12 35 99 18 76 These 5 numbers from the big to the small sort Third, the idea: (1) Compare the size of the 1th and 2nd digits first, 12<35, because the smaller the hope, so to adjust the order of the two, the result after the exchange:(2) Now compare 2nd and 3rd bit size, 12<99, so need to exchange position, the result of Exchange is: (3) Then compare the size of the 3rd and 4th digits, 12<18, after the exchange results are: (4) Finally comparing the size of the 4th and 5th digits, 12<76, after the exchange results are: (5) after 4 times We find that the smallest of the 5 numbers is in place, each one of which we call "a trip"; (6) Now we start the second trip, the goal is to return the 2nd small number, according to the previous logic, or from the 1th number and 2nd number to start comparing:35 99 18 76 12--①-->-18 76 12 --②-->- 76 12--③--> 99 35 aboutin the first trip compared to know that the 5th is the smallest, so the 4th bit does not compare with 5th, this trip only 3 times compared(7) 3rd trip: 18 12 (compare 2 times )----- /c5> (8) 4th trip: Six-------- 35 18 12, 4 numbers are in place, so the last number is not to be compared, it is the largest "Summary" If there are n number of orders, just the number of n-1, that is, to carry out the n-1, and each trip starts from the 1th place adjacent to the two number of comparisons, the small number is placed in the back, has been returned without comparison. The core part of the "feature" bubbling algorithm is the double nested loop, which shows that the time complexity is O (N2), which is a very high time complexity. Here you use Java to give a simple algorithm process.
/*** Bubble sort, small to large *@paramargs*/ Public Static voidMain (String args[]) {intNums[] =New int[] {12, 35, 99, 18, 76,2,4,5,6,77,8,9 }; for(inti=0;i<nums.length-1;i++){ //int j=0;j<nums.length-1;j++//int j=0;j<nums.length-1-i;j++//second, excellent and the first kind for(intj=0;j<nums.length-1-i;j++){ intF=NUMS[J];//First Number intS=NUMS[J+1];//Second number if(s<f) { inttemp=F; NUMS[J]=nums[j+1]; Nums[j+1]=temp; }}} output (Nums); } Public Static voidOutputintnum[]) { for(intn:num) {System.out.print (n+" "); } }Quick Sort (witness the love Journey of Adam and Eve)First, scene: 6 1 2 7 9 3 4 5 10 8 This 10 number is sorted Second, the idea: first find a base number (a reference to the number), for convenience, we choose the leftmost 6, hoping to put >6 to the right of 6, <6 to 6 left. such as: 3 1 2 5 4 6 9 7, 8 first assume that you need to move 6 to the position of K,k to the left of the number <6, the number on the right >6(1) We start "probing" from both ends of the initial sequence "6 1 2 7 9 3 4, 5 10 8", first find a <6 number from the right to the left, then find a >6 number from left to right , then swap. We use the variable i and the variable J to point to the leftmost and rightmost of the sequence. At first, the leftmost i=0 points to 6, and the rightmost j=9 points to 8. (2) The base number is now set to the leftmost number, so the sequence first right to the left (j--), when found a <6 number (5) to stop. Then the sequence moves from left to right (i++) until a number of >6 is found and stops (7);(3) Exchange of the two, results: 6 1 2 5 9 3 4 7, 8; (4) The position of J continues to move to the left ( friendly tip: Each time must start from the position of J ), found that 4 meet the requirements, then i++ found that 9 meet the requirements, the results of the Exchange: 6 1 2 5 4 3 9 7 8; (5) At present, J points to a value of 9,i pointed to the value of 4,j--found 3 meet the requirements, then i++ found I=j, indicating that this round of movement is over. Now the base number 6 and 3 are exchanged, the result:3 1 2 5 4 6 9 7 ten 8; now 6 The left is <6, and the number on the right is >6, but the game is not over (6) We take the number of the left of 6 out first: 3 1 2 5 4, this time with 3 as the base number to adjust, so that 3 left the number of <3, the right number of >3, according to the previous simulation, this time results: 2 1 3 5 4(7) Then 2 1 key out to reorganize, the results obtained: 1 2(8) The remaining sequence on the right: 9 7 10 8 That's the way to go, the final result: 1 2 3 4 5 6 7 8 9 (specific) each round of fast sorting is actually the number of the base of this round, and when all the numbers are in place, the order is over.Java code implementation:
/*** 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 } /** * * @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 } } /*** Quick Sort * *@paramnumbers * with sorted array*/ Public Static voidQuickint[] numbers) { //to see if an array is empty if(Numbers.length > 0) {QuickSort (numbers,0, Numbers.length-1); } } /*** Print *@paramnumbers*/ Public Static voidPrintarr (int[] numbers) { for(inti = 0; i < numbers.length; i++) {System.out.print (Numbers[i]+ ","); } System.out.println (""); } Public Static voidMain (string[] args) {int[] numbers = {10, 20, 15, 0, 6, 7, 2, 1,-5, 55 }; System.out.print ("Before sorting:"); Printarr (numbers); Quick (numbers); System.out.print ("After quick sorting:"); Printarr (numbers); }
Reference article:
Http://www.cnblogs.com/liqingwen/p/4994261.html http://www.cnblogs.com/0201zcr/p/4763806.html
Java Sorting (grooming)