Several common sorting algorithms review one, simple sorting method1. Direct Insert Sort
Basic idea : sequentially inserts the record to be sorted into the appropriate position of the sorted record subsequence by the size of its key code.
Algorithm Code :
//Direct insert sort public static void Insertsort (seqlist<int> seq) {if (seq. IsEmpty () | | Seq. GetLength () = = 1) return; Console.Write ("1.1 Simple Sort before:"); Seq. Display (); int tmp; for (int i = 1; i < seq. GetLength (); i++) {if (Seq[i] < seq[i-1]) {tmp = Seq[i]; Int J; for (j = i-1; J >= 0 && tmp < SEQ[J]; j--) {seq[j + 1] = Seq[j] ; } seq[j + 1] = tmp; } console.write ("I={0}", i); Seq. Display (); } }
Note: The seqlist<t> here is a custom order table, which is not detailed here.
The complexity of direct insertion sequencing is related to the ordering of sequential tables, which are basically between O (n) and O (N2). The algorithm is stable. If the number of sorted records is less than n (for example, n≤50), direct insertion can be used to sort or simplify
Single-selection sort.
2. Bubble sort
basic idea : Compare the key codes of the adjacent records, if the key codes previously recorded are larger than the key codes that are 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 Bubble sort before:"); 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 : Best O (n), worst O (n^2). It is stable.
3. Simple Sorting algorithm
Basic idea : every trip in N-i+1 (i=1,2,..., n-1) records, select the smallest record of the keyword as the first record of the ordered sequence.
Algorithm Code :
Simple select sort public static void Simpleselectsort (seqlist<int> seq) { if (seq). IsEmpty () | | Seq. GetLength () = = 1) return; Console.Write ("1.3 Simple selection before 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;//Find minimum element subscript } } tmp = seq[i];//Interchange Seq[i] = Seq[index]; Seq[index] = tmp; Console.Write ("i={0} ", i); Seq. Display (); } }
complexity of Time: O (n^2), it is a stable sort. If the initial state of the record has been basically ordered according to the key code, you can use direct insert sort or bubble sort.
Second, quick sort
basic idea : by continuously comparing the key code, a record is bounded (the record is called the Fulcrum), the waiting sequence is divided into two parts. Where the key code that satisfies all the records is greater than or equal to the key of the pivot record, and the key code of the other part is less than the key code of the pivot record. The process of dividing the waiting sequence into two parts according to the key code is called a division by the pivot record as the boundary. The sections are continuously divided until the entire sequence is ordered according to the key code.
Algorithm Code :
Quick sort, recursive, with first element as fulcrum public static void QuickSort (seqlist<int> seqlist, int low, int high) { int Tmplow = low;//temporarily saves the opening element index int tmphigh = high;//Temporary Save end Element index int tmp = Seqlist[low]; if (low >=) return; while (Low < high) {when (Low < high && Seqlist[high] >= tmp) high--; Seqlist[low] = Seqlist[high]; while (Low < high && Seqlist[low] <= tmp) low++; Seqlist[high] = Seqlist[low]; } Seqlist[low] = tmp; Seqlist. Display (); QuickSort (Seqlist, Tmplow, low-1); QuickSort (seqlist, low + 1, tmphigh); }
time Complexity: The average performance of the fast sorting method is the best, the time complexity is O (nlog2n), and the fast sort is most suitable when the sequencing sequence has been randomly distributed by key code. But fast sequencing in the worst case time complexity is O (N2). The quick Sort method is an unstable sort method.
Review of several common sorting algorithms