Classical algorithm Learning--Quick sorting

Source: Internet
Author: User

Quick sorting should be considered the most commonly used algorithm in the interview test, and the interviewers are very fond of it. The efficiency of sorting is more efficient in the same O (N*logn), so it is often adopted, and the thought of it is also used in the thought of division and recursion. Sample code upload to: Https://github.com/chenyufeng1991/QuickSort

The basic idea of the algorithm is:

(1) First take a number from the series as the base number (often choose the first number);

(2) The partitioning process, which is placed on the right side of the number, is less than or equal to its left;

(3) Repeat the second step to the left and right intervals until there is only one number position for each interval, that is, the left border subscript is equal to the bottom of the border;


The simplified description is:

1.i= L, J=r, base number is a[i], save it;

2.J--, from the back forward to find a smaller number than it, and then put this number in a[i];

3.i++, from the front to find a larger number than it, found after the number of fill into the a[j];

4. Recursively perform 2, 32 steps until i==j, and finally fill in the base number into a[i];

The specific code is implemented as follows:

main.c//quicksort////Created by Chenyufeng on 16/1/27.//copyright©2016 year chenyufengweb. All rights reserved.//#include <stdio.h>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] = {3,6,0,9,4}; Using a pointer to return an array, the returned array is actually the head pointer;/** * uses the return pointer; *///int *retarr;//Retarr = quickSort (Numarr, 0, 4);/For (I NT i = 0; I < 5; i++) {///////printf ("%d", * (Retarr + i))//}/** * Direct reference, more convenient; */quickSort02 (Numar    R, 0, 4);    for (int i = 0; i < 5; i++) {printf ("%d", numarr[i]); }}int *quicksort (int arr[],int l,int R) {//when the left and right pointers are equal, return directly; if (L < R) {//X at this time is the reference value; int i = L,j =        R,x = Arr[l]; The following while loop represents one-time division, that is, a single order, while (I < j) {/////To find values less than the datum from the right side of the reference value; while (I < J &&amp ;            ARR[J] >= x) {j--;          } if (I < j) {      Exchange order, i++; arr[i++] = arr[j];            }//To find values greater than the baseline from the left of the base value; while (I < J && Arr[i] < x) {i++;                } if (I < J) {//Exchange order, j--;            arr[j--] = Arr[i];        }}//Place reference value into arr[i] position; arr[i] = x;        Recursion, the left and right sides are respectively QuickSort (arr, L, i-1);    QuickSort (arr, i + 1, R); } return arr;} void quickSort02 (int *arr,int l,int R) {//when the left and right pointers are equal, return directly; if (L < R) {//X at this time is the reference value; int i = L,j = R        , x = Arr[l]; The following while loop represents one-time division, that is, a single order, while (I < j) {/////To find values less than the datum from the right side of the reference value; while (I < J &&amp ;            ARR[J] >= x) {j--;            } if (I < J) {//Exchange order, i++; arr[i++] = arr[j];            }//To find values greater than the baseline from the left of the base value; while (I < J && Arr[i] < x) {i++; } if (I &LT                j) {//Exchange order, j--;            arr[j--] = Arr[i];        }}//Place reference value into arr[i] position; arr[i] = x;        Recursion, the left and right sides are respectively QuickSort (arr, L, i-1);    QuickSort (arr, i + 1, R); }}



This article references: http://blog.csdn.net/morewindows/article/details/6684558

Classical algorithm Learning--Quick sorting

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.