Recursive quick sorting

Source: Internet
Author: User
Quick sorting is an improvement of bubble sorting. Its basic idea is: split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by time. The entire sorting process can be recursive or non-recursive to convert the entire data into an ordered sequence.

Quick sorting is an improvement of bubble sorting. Its basic idea is: split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by time. The entire sorting process can be recursive or non-recursive to convert the entire data into an ordered sequence.

The recursive implementation of quick sorting is as follows:

# Include "stdafx. h "# define LIST_INIT_SIZE 100 // initial size of the sequence table # define LISTINCREMENT 10 // incremental typedef int ElemType of the sequence table; // ordered table element type typedef struct // ordered table structure {ElemType * elem; int length; int listsize;} SqList; // initialize the ordered table int InitList_Sq (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); if (! L. elem) return-1; L. length = 0; L. listsize = LIST_INIT_SIZE; return 0 ;}// create an ordered table int CreatList_Sq (SqList & L) {InitList_Sq (L); int I = 1; while (scanf ("% d ", & L. elem [I])! = EOF) {I ++;} L. length = I-1; return 0;} // A quick sorting int Partition (SqList & L, int low, int high) {L. elem [0] = L. elem [low]; int foreign TKEY; foreign TKEY = L. elem [low]; int temp; while (low  

Non-recursive algorithm for fast sorting:

# Include
 
  
# Include
  
   
Using namespace std; template
   
    
Int partition (T a [], int low, int high) {T v = a [low]; while (low
    
     
= V) high --; a [low] = a [high]; while (low
     
      
Void QuickSort (T a [], int p, int q) {stack
      
        St; int j; do {while (p
       
         This article is available at http://www.nowamagic.net/librarys/veda/detail/1194.
        

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.