Common sorting algorithms for interview questions

Source: Internet
Author: User

The default sorting effect of the following sorting sequence is from small to large. The sequence to be sorted: 3,4, 63,4,-9, 0,-21. basic Idea of Bubble Sorting: Exchange adjacent two elements in sequence, so that big data is sunk (or small data is floated up) Step 1: Compare adjacent two elements, if the former is larger than the latter, two elements are exchanged. Otherwise, do not switch. Step 2: repeat the first step until the last two elements are compared. At this time, the largest element is at the end, and this sorting is completed. Step 3: remove the last element and repeat the preceding two steps to sort the data before the last element. Step 4: after each sorting is completed, big data will be forgotten to sink, that is, the data to be sorted will be fewer and fewer until there is no data to be sorted. Sample Code for sorting success: [java] public void bullle (int [] array) {// double-layer loop. The first layer controls the number of partitions to be sorted for (int I = 0; I <array. length-1; I ++) {// second layer, to the first until array. data Sorting for (int j = 0; j <array. length-i-1; j ++) {// if (array [j]> array [j + 1]) {int temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp ;}}} 2. basic Idea of cocktail sorting: The cocktail sorting method is equivalent to two-way Bubble sorting. In the cocktail sorting, the big data goes down from left to right. Sink, and make small data float up from the right to the left. Process example: code example: [java] public void cocktailSort (int [] array) {int temp; for (int k = 0; k <array. length/2; k ++) {// sink large data for (int I = k; I <array. length-k-1; I ++) {if (array [I]> array [I + 1]) {temp = array [I]; array [I] = array [I + 1]; array [I + 1] = temp ;}/// float small data up (int j = array. length-2-k; j> k; j --) {if (array [j] <array [J-1]) {temp = array [j]; array [j] = array [J-1]; array [J-1] = temp ;}}} 3. quick sorting Think: sort the sequence by a trip. The sequence is divided into two parts. All the data in one part is smaller than the other part, and then sort the two parts by this method, the entire sorting process can be recursively used for the first step: select a reference value (usually the first element of the column to be sorted), and record it so that I, j points to the first element and the last element. Step 2: Start from the right book and compare the size of j points to the element and the reference value. If it is greater than, j moves an element forward, until an element smaller than the reference value is found, the movement of j is stopped. In this case, assign the element that j points to the element that I points to, And let I move an element backward. Step 3: Start from the left and compare the size of the elements pointing to the I and the reference value. If the value is smaller than that, I moves an element backward until an element greater than the reference value is found, stop the I movement. In this case, assign the element I points to the element j points to, And let j move an element forward. Step 4: Repeat the preceding steps to know That I and j point to the same element, and assign the reference value to the elements pointed to by I and j. At this time, the sequence has been divided into two parts by the reference value. All elements on the left side are smaller than the reference value, and all elements on the right side are greater than the reference value. Step 5: use the same method, sample Code: [java] private void quickSort (int [] array, int start, int end) {int I = start, j = end; int temp = array [start]; // record the reference value while (I <j) {// start from the right, find the first element that is less than the reference value while (I <j & array [j]> temp) {j --;} // assign the element where j is located to the element pointed to by I and move I backward if (I <j) {array [I] = array [j]; I ++;} // start from the left and find the first element that is greater than the benchmark value while (I <j & array [I] <temp) {I ++ ;} // Assign the element where I is located to the element pointed to by j, and move j forward if (I <j) {array [j] = array [I]; j --; }}// when I> = j, the loop query ends. Assign the reference value to the position where I is located. array [I] = temp; // sort the two parts of the Base Value split if (I-1> start) {quickSort (array, start, I-1);} if (I + 1 <end) {quickSort (array, I + 1, end) ;}} 4. the basic idea of insertion sorting: Sorting sequence is divided into two parts. The first part is the sorted sequence, and the last part is the sequence to be sorted. Step 1: select an element from the sequence to be sorted, and compare the size of the elements in the sequence with that in the sorted sequence until you find an element larger than it, insert it to the front of the element. Step 2: Repeat the previous step until there is no element in the sequence to be sorted. Example of sorting process: code example: [java] public void InsertSort (int [] array) {for (int I = 1; I <array. length; I ++) {/** first method, search from the past to the next, until an element greater than the element * is found, and then all elements in the sorted sequence are moved backward. ** // for (int j = 0; j <I; j ++) {// if (array [I] <array [j]) {// int temp = array [I]; // int k = I; // while (k> j) {// array [k] = array [k-1]; // k --; //} // array [j] = temp; // break; //}/** method 2, search from the back to the front, until a smaller element * is found, the elements in all sorted sequences are moved to the backend. Dynamic **/int temp = array [I]; int j = I; if (array [J-1]> temp) {while (j> = 1 & array [J-1]> temp) {array [j] = array [J-1]; j --;}} // assign this element to the corresponding position array [j] = temp ;}} 5. select sorting. This option looks very similar to insert sorting. It selects an element from a sequence that has never been sorted and then inserts it into the sorted sequence. However, they are quite different. The selection of sorting is to select a minimum element from the sequence to be sorted, and then insert this element to the end of the sorted sequence. The advantage of this method is that, you do not need to move data frequently in the array. You only need to exchange data in pairs. Basic Idea: Step 1: Find the smallest element from the sequence to be sorted by comparison and record its position and value Step 2: swap this element with the first element of the sequence to be sorted (that is, the next element of the last element of the sequence to be sorted. Repeat the preceding two steps until all elements have been arranged. Example of sorting process: code example: [java] public void selectSort (int [] array) {or (int I = 0; I <array. length-1; I ++) {int loc = I; int min = array [I]; // find the smallest element for (int j = I + 1; j <array. length; j ++) {if (min> array [j]) {min = array [j]; loc = j ;}} if (loc! = I) {array [loc] = array [I]; array [I] = min ;}} 6. the basic idea of merging and sorting: divides the sequence to be sorted into two parts, sorts the two parts separately, and finally merges the two parts in a certain way. The sorting process is a recursive process. The merge method is a typical representative of the algorithm's divide and conquer method. Therefore, Merge Sorting is also divided into two parts: the process of division and the process of governance (Merger. The merge method is to compare the first element of two queues, remove a small one from the queue and insert it to the first position of the new queue, then, after comparing the size of the first element of the two queues, remove the small one from the queue and add it to the end of the last element added to the new queue, repeat Step 2 to know that a queue is empty. Add all the remaining elements of a non-empty queue to the end of the new queue in sequence. Sorting Process example: the first split: {3, 4, 63, 2} {-9, 0, 1, 32,-2}. The first part after the split is used as an example. The second split: {3, 4} {63, 2} The third split: {3} {4} {63} {2} The merge process: first merge: {3, 4} {2, 63} second merge: {2, 3, 4} now, the first part has been sorted, and the second part has the same sorting method, which is {-9, -,}: {-9,-,}. code example: [java] private void MergeSort (int [] array, int start, int end) {if (start = end) {return;} int middle = (end-start + 1)/2 + start; // The process of splitting MergeSort (array, start, middle-1); MergeSort (array, middle, end); // the process of merging merge (array, start, middle, end);} private void merge (int [] array, int start, int middle, int end) {int I = start, j = middle, loc = 0; int [] copy = new int [end-start + 1]; while (I <middle & j <= end) {if (array [I]> array [j]) {copy [loc] = array [j]; j ++;} else {copy [loc] = array [I]; I ++;} loc ++ ;} while (I <middle) {copy [loc ++] = array [I ++];} while (j <= end) {copy [loc ++] = array [j ++];} for (int k = 0; k <copy. length; k ++) {array [start ++] = copy [k];} copy = null ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.