Classic algorithm learning-fast sorting. Classic algorithm learning-quick sorting and quick sorting are the most commonly used algorithms in the interview test. The sorting efficiency is the same as that of O (N * logN ).
Quick sorting is the most commonly used algorithm in the interview test. Sorting efficiency is highly efficient among several sorting methods that are the same as O (N * logN). Therefore, this method is often used, and the idea is also the idea of partitioning and recursion. Sample code uploaded to: https://github.com/chenyufeng1991/QuickSort
The basic idea of an algorithm is:
(1) first extract a number from the series as the reference number (the first number is often selected );
(2) in the partitioning process, place a number larger than this number to its right, and all the numbers smaller than or equal to this number to its left;
(3) repeat the second step on the left and right intervals until each interval has only one number position, that is, the left boundary subscript is equal to the right boundary subscript;
Simplified description:
1. I = L, j = R, and the reference number is a [I]. save it;
2. j --, locate a smaller number than it in the forward direction, and put the number in a [I;
3. I ++: find a larger number from the front to the back, and enter this number in a [j;
4. perform step 2 and 3 recursively until I = j and enter the reference number in a [I;
The code is as follows:
/// Main. c // QuickSort /// Created by chenyufeng on 16/1/27. // Copyright©2016 chenyufengweb. All rights reserved. // # include
Int * quickSort (int arr [], int l, int r); void quickSort02 (int * arr, int l, int r); int main (int argc, const char * argv []) {int numArr [5] = {, 4}; // returns an array using a pointer, which is actually the header pointer of an array; /*** use the returned pointer; * // int * retArr; // retArr = quickSort (numArr, 0, 4); // for (int I = 0; I <5; I ++) {// Obtain the array value // printf ("% d", * (retArr + I )); ///}/*** it is convenient to directly pass the reference. */quickSort02 (numArr, 0, 4); for (int I = 0; I <5; I ++) {printf ("% d", numArr [I]) ;}} int * quickSort (int arr [], int l, int r) {// return directly when the left and right pointers are equal; if (l <r) {// x is the reference value; int I = l, j = r, x = arr [l]; // the while loop below indicates one grouping, that is, one sorting; while (I <j) {// first find the value smaller than the benchmark value from the right of the benchmark value; while (I <j & arr [j]> = x) {j --;} if (I <j) {// exchange order, I ++; arr [I ++] = arr [j];} // find a value greater than the benchmark value on the left of the benchmark value; while (I <j & arr [I] <x) {I ++;} if (I <j) {// exchange order, j --; arr [j --] = arr [I] ;}// place the reference value in the position of arr [I]; arr [I] = x; // recursion, fast sorting is performed on both sides of the left and right sides; quickSort (arr, l, I-1); quickSort (arr, I + 1, r);} return arr ;} void quickSort02 (int * arr, int l, int r) {// returns directly when the left and right pointers are equal; if (l <r) {// at this time, x is the reference value; int I = l, j = r, x = arr [l]; // the while loop below indicates a sub-governance, that is to say, sort it once; while (I <j) {// first finds the value smaller than the benchmark value from the right of the benchmark value; while (I <j & arr [j]> = x) {j --;} if (I <j) {// exchange order, I ++; arr [I ++] = arr [j];} // locate the value greater than the benchmark value on the left of the benchmark value. while (I <j & arr [I] <x) {I ++;} if (I <j) {// exchange order, j --; arr [j --] = arr [I] ;}} // place the reference value in the position of arr [I]; arr [I] = x; // recursive. the left and right sides are arranged in the fast rank; quickSort (arr, l, i-1); quickSort (arr, I + 1, r );}}
The sort quick sorting algorithm is the most commonly used algorithm in the interview test. interviewers like it very much. The sorting efficiency is the same as that of O (N * logN...