Common sorting algorithms

Source: Internet
Author: User
ArticleDirectory
    • I. Simple sorting method
    • Ii. Quick sorting
1. Simple sorting method 1. Insert directly for sorting

Basic Ideas: Insert the records to be sorted in sequence to the appropriate position of the sorted records subsequence according to their key code.

AlgorithmCode:

 // directly Insert the sorted public static void insertsort (seqlist 
  
    SEQ) {If (seq. isempty () | seq. getlength () = 1) return; console. write ("1.1 before simple sorting:"); seq. display (); int TMP; For (INT I = 1; I 
   
     = 0 & TMP 
    
   
  
Note: The seqlist <t> here is a custom sequence table, which is not detailed here.

The complexity of directly inserting the sorting time is related to the order of the sequence table, which is basically between O (N) and O (n2. This algorithm is stable. If the number of sorting records is less than or equal to n (for example, n ≤ 50), you can directly Insert the sorting or
Select order.

2. Bubble Sorting

Basic Ideas: Compare key codes of adjacent records. If the key codes recorded earlier are greater than the key codes recorded later, they are exchanged. Otherwise, they are not exchanged.

Algorithm code:

// Bubble sort public static void bubblesort (seqlist <int> SEQ) {If (seq. isempty () | seq. getlength () = 1) return; console. write ("1.2 before Bubble Sorting:"); seq. display (); int TMP; For (INT I = 0; I <seq. last; I ++) {for (Int J = seq. last-1; j> = I; j --) {If (SEQ [J + 1] <seq [J]) {TMP = seq [J]; SEQ [J] = seq [J + 1]; seq [J + 1] = TMP ;}} console. write ("I = {0}", I); seq. display ();}}

Time Complexity: The best O (n), the worst O (N ^ 2 ). It is stable.

3. Simple Sorting Algorithm

Basic Ideas: Each trip is in n-I + 1 (I = 1, 2 ,..., N-1) records with the smallest keyword are selected as the I-th record of the ordered sequence.

Algorithm code:

// Simply select and sort public static void simpleselectsort (seqlist <int> SEQ) {If (seq. isempty () | seq. getlength () = 1) return; console. write ("1.3 before simple sorting:"); seq. display (); int TMP; int index; For (INT I = 0; I <seq. last; I ++) {Index = I; for (Int J = I; j <= seq. last; j ++) {If (SEQ [J] <seq [Index]) {Index = J; // locate the smallest element subscript} TMP = seq [I]; // exchange seq [I] = seq [Index]; seq [Index] = TMP; console. write ("I = {0}", I); seq. display ();}}

Time Complexity:O (N ^ 2), which is a stable sorting. If the initial status of a record has been sorted by the key code, you can sort it by direct insertion or bubble.

Ii. Quick sorting

Basic Ideas: The key code is constantly compared to define a record (this record is called the pivot point), and the columns to be sorted are divided into two parts. Among them, some key codes that meet the requirements of all records are greater than or equal to the key code of the Fulcrum record, and the other key codes of the record are smaller than the key code of the Fulcrum record. The process of dividing the columns to be sorted by key code into two parts by the pivot record is called a division. The parts are constantly divided until the entire sequence is ordered by the key code.

Algorithm code:

// Fast sorting and recursion. Use the first element as the fulcrum public static void quicksort (seqlist <int> seqlist, int low, int high) {int tmplow = low; // temporarily Save the start element index int tmphigh = high; // temporarily Save the end element index int TMP = seqlist [low]; If (low> = high) return; while (low  

time complexity: the average performance of the fast sorting method is the best, and the time complexity is O (nlog2n ), when the sorted sequence is already randomly distributed by key code, quick sorting is the most suitable. However, in the worst case, the time complexity is O (n2 ). The quick sorting method is an unstable sorting method.

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.