Various Sorting Algorithm
# Include <stdio.h>
# Include <stdlib.h>
# Include <time.h>
# Include <conio.h>
# Include <iostream.h>
25000 # define N / / number of elements that sort
Void insertsort (int R [N +1]) / / direct insertion sort
(
Int i, j;
For (i = 2; i <= N; i + +) (
R = R [0] [i]; / / set up surveillance posts
J = i-1;
While (R [0] <R [j]) (
R [j +1] [j] = R;
J -;
)
R [j +1] = R [0];
)
)
Void shellsort (int R [N +1]) / / Sort Hill
(
Int i, j, the gap;
Int x;
Gap = N / 2; / / Set the initial increment
While (gap> 0) (
For (i = gap +1; i <= N; i + +) (
J = i-gap;
While (j> 0)
If (R [j]> R [j + gap]) (
X = R [j];
R = R [j] [j + gap];
R [j + gap] = x;
J = j-gap;
)
Else
J = 0;
)
Gap = gap / 2; / / incremental decrease
)
)
Void bubblesort (int R [N +1]) / / bubble sort
(
Int i, j, noswap;
Int temp;
For (i = 1; i <= N-1; i + +) (
Noswap = 1;
For (j = N; j> = i +1; j -)
If (R [j] <R [j-1]) (
Temp = R [j];
R = R [j] [j-1];
R [j-1] = temp;
Noswap = 0;
)
If (noswap)
Break;
)
)
Int partition (int R [N +1], int low, int high) / / Quick Sort Functions (from the pivotal element)
(
Int i, j;
I = low;
J = high;
R = R [0] [low] / / from the pivotal element
Do (/ / from the two ends of the turn table to the middle scan
While ((j> i) & (R [j]> [0] = R))
J -;
If (i <j) (
R = R [i] [j];
I + +;
)
While ((i <j) & (R [i] <= R [0]))
I + +;
If (i <j) (
R = R [j] [i];
J -;
)
) While (i <j);
R = R [i] [0]; / / pivotal element in place
Return i; / / pivot position
)
Void quicksort (int R [N +1], int low, int high) / / Quick Sort
(
Int i;
If (low <high) (
I = partition (R, low, high); / / R will be divided into two tables
Quicksort (R, low, i-1) / / Table of low-order recursive
Quicksort (R, i +1, high); / / sub-table of high-ranking recursive
)
)
Void selectsort (int R [N +1]) / / Sort direct selection
(
Int i, j, k;
Int temp;
For (i = 1; i <= N-1; i + +) (
K = i;
For (j = i +1; j <= N; j + +)
If (R [j] <R [k])
K = j; / / k pointed out that with each trip interval of disorder in the smallest element
If (k! = I) (
Temp = R [i];
R = R [i] [k];
R [k] = temp;
)
)
)
Void sift (int R [N +1], int s, int m) / / Heap Sort of Functions (screening algorithm, the R [s.. M] become a big heap root)
(
Int i, j;
Int temp;
Temp = R [s];
I = s;
J = 2 * i; / / R R [j] [i] is the left child
While (j <= m) (
If ((j <m) & & (R [j] <R [j +1]))
J + +; / / if the right children larger j revised to put the child's right subscript
If (temp <R [j]) (
R = R [i] [j]; / / R [j] will be transferred to his father's position
I = j;
J = 2 * i; / / modify the value of i and j, to continue downward Screening
)
Else
Break; / / Screening complete termination cycle
)
R [i] = temp; / / Screen node Add to the value of the final position
)
Void heapsort (int R [N +1]) / / Heap Sort
(
Int i;
Int temp;
For (i = N / 2; i> = 1; i -)
Sift (R, i, N) / / set up the initial heap
For (i = N; i> = 2; i -) (/ / N-1 of the cycle, and Heap Sort
Temp = R [1];
R = R [1] [i];
R [i] = temp; / / will be the first element with the current range for the last element
Sift (R, 1, i-1) / / R [1] Screening nodes, by (N-1) nodes of the heap
)
)
Void main ()
(
Int R [N +1], RR [N +1]; / / element to be sort of group
Clock_t start and finish; / / function for the operation of the mind,
Double duration;
Int i;
Cout << "The initial data:" <<endl;
For (i = 1; i <= N; i + +) (
R [i] = rand ()% 5001;
RR = R [i] [i];
Cout <<R [i] << "";
If (i == 0% 10)
Cout <<endl;
)
////////////////////////////////////////////////// /////////
Getchar (); / / direct insertion sort
Start = clock (); / / mind when
Insertsort (RR);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the insert sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
Getchar () / / Sort Hill
For (i = 1; i <= N; i + +) (
RR = R [i] [i];
)
Start = clock (); / / mind when
Shellsort (RR);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the shell sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
Getchar (); / / bubble sort
For (i = 1; i <= N; i + +) (
RR = R [i] [i];
)
Start = clock (); / / mind when
Bubblesort (RR);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the shell sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
Getchar (); / / Quick Sort
For (i = 1; i <= N; i + +) (
RR = R [i] [i];
)
Start = clock (); / / mind when
Quicksort (RR 1, N);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the shell sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
Getchar () / / Sort direct selection
For (i = 1; i <= N; i + +) (
RR = R [i] [i];
)
Start = clock (); / / mind when
Selectsort (RR);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the shell sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
Getchar (); / / Heap Sort
For (i = 1; i <= N; i + +) (
RR = R [i] [i];
)
Start = clock (); / / mind when
Heapsort (RR);
Finish = clock (); / / Hutchison o'clock
Duration = (double) (start-finish) / CLOCKS_PER_SEC;
Cout << "The result based on the shell sort is:" <<endl;
For (i = 1; i <= N; i + +) (
Cout <<RR [i] << "";
If (i == 0% 10)
Cout <<endl;
)
Cout << "The Run Time is:" <<duration << "seconds" <<endl;
////////////////////////////////////////////////// /////////
)
/ * Bubble sort of * /
For (i = 0; i <NUM-1; i + +) / * the circle: control over several times * /
For (j = NUM-1; j> i; j -) / * loop: for every trip compared * /
If (data [j] <data [j-1]) / * If data [j]> data [j-1], the location of the two exchanged * /
(Temp = data [j];
Data [j] = data [j-1];
Data [j-1] = temp;
);








0 Comments to “Various Sorting Algorithm”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.