Comparison of internal sorting algorithms

Source: Internet
Author: User
Sorting algorithms are classic content of the data structure discipline. There are many existing internal sorting algorithms. What are their respective characteristics? This article tries to design and implement common internal sorting algorithms and compare them. They are Bubble sorting, direct insertion sorting, simple selection sorting, fast sorting, heap sorting, and test and compare the comparison times and moving times of keywords.

Problem Analysis and overall designADT orderablelist {
Data Object: D = {ai | ai ε integerset, I = ,..., N, N ≥ 0}
Data Relationship: r1 = {<ai-1, AI> | ai-1, AI, D, I = ,..., N}
Basic operations:
Initlist (N)
Operation Result: Construct an element with the length of N, which is 1, 2 ,..., N.
Randomizel (D, isinverseorser)
Operation Result: Random disruption
Bubblesort ()
Operation Result: Bubble Sorting
Insersort ()
Operation Result: Sort inserts.
Selectsort ()
Operation Result: select and sort
Quicksort ()
Operation Result: quick sorting
Heapsort ()
Operation Result: heap sorting
Listtraverse (visit ())
Operation Result: the visit () function is called for each element of the L type in sequence ()
} ADT orderablelist

The key words of the elements in the table to be sorted are integers. Test and compare the elements in positive, reverse, and different levels of disorder,
Compare the number of comparisons and moves of keywords (the keyword exchange rate is three moves.
A prompt is required. You can enter the table length (100-1000) and the number of groups of different test data (8-18) on the keyboard ). after each test is completed, the columns must be compared.
Analyze the results.

Detailed Design
1. Bubble Sorting
Algorithm: the core idea is to scan the data list to find two adjacent projects in disordered order. After the two items are found, switch the project location and continue scanning. Repeat the preceding operations until all projects are sorted in order (struct rec R [], int N)
{
Int I, J;
Struct rec W;
Unsigned long int compare = 0, move = 0;
For (I = 1; I <= n-1; I ++)
For (j = N; j> = I + 1; j --)
{
If (R [J]. Key <R [J-1]. Key)
{
W = R [J];
R [J] = R [J-1];
R [J-1] = W;
Move = move + 3;
}
Compare ++;
}
Printf ("/nbubblesort compare = % lD, move = % LD/N", compare, move );
}

2. Insert directly to sort
Algorithm: After I-1 processing, L [1 .. I-1] has been sorted. I-repeat only inserts L [I] into the proper position of L [1 .. I-1] so that l [1 .. I] is a sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L [I] and L [I-1], if l [I-1] ≤ L [I], then l [1 .. i] the order has been sorted, the I-th processing is over; otherwise, the position of switching L [I] and L [I-1] continues to compare L [I-1] and L [I-2], until a location J (1 ≤ j ≤ i-1) is found so that insertsort (struct rec R [], int N)
{
Int I, J;
Unsigned long int compare = 0, move = 0;
For (I = 2; I <= N; I ++)
{Compare ++;
R [0] = R [I];
Move ++;
J = I-1;
While (R [0]. Key {R [J + 1] = R [J];
J --;
Move ++;
++ Compare ;}
R [J + 1] = R [0];
Move ++;
}
Printf ("/ninsertsort compare = % lD, move = % LD/N", compare, move );
}

3. Simple selection and sorting
Algorithm: First, find the smallest data in the data list, and then change the data to the first data exchange location. Then, find the second small data, change it to the second data exchange location, and so on. Selectsort (struct rec R [], int N)
{
Unsigned long int compare = 0, move = 0;
Int I, J, K;
Struct rec W;
For (I = 1; I <= n-1; I ++)
{K = I;
For (j = I + 1; j <= N; j ++)

{If (R [J]. Key> r [K]. Key) {k = J; compare ++ ;}
W = R [I];
R [I] = R [k];
R [k] = W;
Move = move + 3;

}
}
Printf ("/nselectsort compare = % lD, move = % LD/N", compare, move );
}

4. Fast sorting
Algorithm: First, check the number of data in the data list. If the number is smaller than two, exit the program directly. If there are more than two pieces of data, select a split point to divide the data into two parts. The data smaller than the split point is placed in one group, and the rest is placed in another group, sort the two groups of data respectively.
Generally, the split point data is randomly selected. In this way, no matter whether your data has been sorted or not, the size of the two word lists you split is similar. As long as the size of the two sublists is about the same

Q (struct rec R [], int S, int T)
{
Int I = s, j = T;
If (S <t)
{
R [0] = R [s]; ++ A; C ++;
Do {
While (j> I & R [J]. Key> = R [0]. Key)
{J --;
+ + ;}
If (I <j)
{R [I] = R [J];
I ++;
C ++ ;}
While (I <J & R [I]. Key <= R [0]. Key)
{I ++;
+ + ;}
If (I <j)
{R [J] = R [I];
J --;
C ++ ;}
} While (I <j );
R [I] = R [0];
C ++;
Q (R, S, J-1 );
Q (R, J + 1, t );
}
}

5. Heap sorting
(1) Basic Ideas:
Heap sorting is a tree-based sorting. During the sorting process .. n] As a Complete Binary Tree ordered storage structure, the use of the full binary tree between the parent node and the child node to select the smallest element.
(2). Heap definition: The sequence of n elements is K1, K2, K3,..., kN. It is called heap. if and only when the sequence satisfies the characteristics:
Ki ≤ k2i Ki ≤ k2i + 1 (1 ≤ I ≤ [n/2])

Sift (struct rec R [], int L, int m)
{
Int I, J;
Struct rec W;
I = L; j = 2 * I;
W = R [I];
While (j <= m)
{
If (j <M & R [J]. Key <R [J + 1]. Key) {J ++;
}
If (W. Key <R [J]. Key)
{
R [I] = R [J];
I = J;
J = 2 * I;
}
Else J = m + 1;
}
R [I] = W;
}

Heapsort (struct rec R [], int N)
{
Unsigned long int compare =-1, move =-1;
Struct rec W;
Int I;
Int;
For (I = n/2; I> = 1; I --) A = sift (R, I, n );
Compare ++;
Move ++;

For (I = N; I> = 2; I --)
{
W = R [I];
R [I] = R [1];
R [1] = W;
A = sift (R, 1, I-1 );
Compare + =;
Move + =;
}
}

Summary:
1. Learn to use the random function randomize () to assign an initial value to the array. Add # include to the header file.
2. Before doing this program, it is basically completed after understanding various sorting processes.
3. Summary of the Sorting Algorithm:
(1) If n is small (such as N ≤ 50), direct insertion or direct selection of sorting can be used.
When the record size is small, the direct insertion sorting is better; otherwise, the sorting should be selected because the number of records to be moved is less than the direct insertion.
(2) If the initial state of the file is basically ordered (positive), direct insertion, bubble or random quick sorting should be selected;
(3) If n is large, the time complexity is O (nlgn.
Quick sorting is the best method in comparison-based internal sorting. When the keyword to be sorted is a random distribution, the average time of quick sorting is the shortest;
The auxiliary space required for heap sorting is less than that for quick sorting, and the worst possible case for quick sorting is not displayed. The two sorting types are unstable.

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.