Sorting Algorithm Analysis and Design experiment

Source: Internet
Author: User

Lab content

Sort and sort the three randomly generated integer sequences (1000 numbers, 10000 numbers, and 100000 numbers ).AlgorithmThe following five typical methods are used: bubble sort algorithm, select sort algorithm, insert sort algorithm, merge sort algorithm, and quick sort algorithm.

Lab Purpose

• Review and familiarize yourself with common sorting algorithms.

• Experiment experiences the profound impact of Algorithm Design on problem solving efficiency.

Basic Idea of Algorithm Design

Select sort

In the number of groups to be sorted, the minimum number is selected to exchange with the number at the first position. Then, the minimum number is used to exchange with the number at the second position, this loops until the second to last number is compared with the last number.

Insert sort directly

In the number of a group to be sorted, assume that the number of the preceding (n-1) [n> = 2] is already sorted in order. Now we need to insert the nth number into the preceding ordered number, so that the n numbers are sorted in order. This repeats until all the rows are sorted.

Bubble Sorting

In the number of groups to be sorted, compare and adjust the two adjacent numbers from top to bottom based on the total number in the range not sorted yet, let a large number sink, a small number rises. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.

Merge Sorting Algorithm

Merge sort is another type of sorting method. The meaning of merging is to combine two or more ordered data sequences into a new ordered data sequence, therefore, it is also called the merge algorithm. The basic idea is to assume that array A has n elements, and array a is composed of N ordered subsequences. The length of each subsequence is 1, then merge them in two ways to obtain an n/2 ordered subsequences with a length of 2 or 1, and then merge them in two, this sorting method is called 2-way Merge Sorting until an ordered data sequence with a length of N is obtained.

Quick sorting

Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.

ProgramList

# Include <stdlib. h>

# Include <stdio. h>

# Include <ctime>

# Include <iostream>

Using namespace STD;

 

Void swap (Int & X, Int & Y) // two data exchanges

{Int temp;

Temp = X;

X = y;

Y = temp;

}

/************************************ Bubble sorting method ****************************/

Void bubblesort (int r [], int length)

{

Int I, j, T;

I = length-1;

While (I> 0)

{T = 0;

For (j = 0; j <I; j ++)

If (R [J + 1] <R [J])

{Swap (R [J], R [J + 1]);

T = J;

}

I = T;

}

}

***** ***************************/

Void selectionsort (int r [], int length)

{Int min, I, j;

For (I = 0; I <length; I ++)

{Min = I;

For (j = I + 1; j <length; j ++)

If (R [J] <R [Min]) min = J;

Swap (R [I], R [Min]);

}

}

/********************************** Direct insert sorting method **** ***************************/

Void insertionsort (int r [], int length)

{Int I, J, K;

For (I = 1; I <length; I ++)

{

If (R [I] <R [I-1])

{K = R [I];

J = I-1;

Do {

R [J + 1] = R [J];

J --;

} While (j> = 0 & K <R [J]);

R [J + 1] = K;

}

}

}

/******************************** Merge Sorting ***** *****************************/

Void merge (INT L1 [], int L2 [], const int left, const int mid, const int right)

// Combine two subsequences into a group column

{

For (int K = left; k <= right; k ++)

L2 [k] = L1 [k];

Int S1 = left, S2 = Mid + 1, t = left; // S1, S2 is the detection pointer, T is the storage pointer

While (S1 <= Mid & S2 <= right) // The two arrays are not completely detected and compared

If (L2 [S1] <= L2 [s2]) L1 [t ++] = L2 [S1 ++];

Else L1 [t ++] = L2 [S2 ++];

While (S1 <= mid) L1 [t ++] = L2 [S1 ++]; // if the first array is completely inspected, copy

While (s2 <= right) L1 [t ++] = L2 [S2 ++]; // if the second array is completely inspected, copy

}

 

Void mergesort (int l [], int L2 [], int left, int right)

{

If (left> = right) return;

Int mid = (left + right)/2; // It is divided into two subsequences from the center.

Mergesort (L, L2, left, mid); // recursively sorts the left subsequence

Mergesort (L, L2, Mid + 1, right); // recursively sorts the subsequences on the right.

Merge (L, L2, left, mid, right); // merge

}

****** ******************************/

Int partition (int l [], int low, int high) // divide the sequence into two subsequences

{Int temp;

L [0] = L [low]; // reference Element

Temp = L [low];

While (low

{While (low <High & L [High]> = temp) -- high;

L [low] = L [High]; // less than the baseline element, switch to the left

While (low <High & L [low] <= temp) ++ low;

L [High] = L [low]; // greater than the baseline element and put it on the right

}

L [low] = L [0];

Return low; // return the reference element.

}

 

Void quicksort (int r [], int left, int right) // fast sorting

{Int reverse tloc;

If (left <right)

{Reverse tloc = partition (R, left, right); // you can specify the center value.

Quicksort (R, left, pivotloc-1); // recursive left

Quicksort (R, repeated tloc + 1, right); // recursive right

}

}

 

Void Exchange (int B [], int C [], int length) // exchange two array values

{Int K;

For (k = 0; k <length; k ++)

B [k] = C [k];

}

 

Void print (int r [], int length) // output sequence

{For (INT I = 0; I <length; I ++)

Cout <R [I] <"";

Cout <Endl;

}

 

Int main ()

{Int I, J;

Int A [100000] = {0}; int N [100000] = {0 };

Int R [] = {1000,10000, 100000 };

Double Start, finish, duration;

Srand (time (0 ));

For (j = 0; j <3; j ++)

{Cout <Endl <R [J] <"Number of five sort cases" <Endl;

For (I = 0; I <R [J]; I ++)

{A [I] = rand () % R [J];

N [I] = A [I];

}

// Print (A, R [J]);

 

Start = clock ();

Bubblesort (A, R [J]);

Finish = clock ();

Duration = (finish-Start)/clocks_per_sec * 1000;

Cout <"bubblesort need the time:" <duration <Endl;

// Print (A, R [J]);

 

Exchange (A, N, R [J]); // ensure that the same array is used for sorting.

Start = clock ();

Selectionsort (A, R [J]);

Finish = clock ();

Duration = (finish-Start)/clocks_per_sec * 1000;

Cout <"selectionsort need the time:" <duration <Endl;

// Print (A, R [J]);

 

Exchange (A, N, R [J]);

Start = clock ();

Insertionsort (A, R [J]);

Finish = clock ();

Duration = (finish-Start)/clocks_per_sec * 1000;

Cout <"insertionsort need the time:" <duration <Endl;

// Print (A, R [J]);

 

Exchange (A, N, R [J]);

Start = clock ();

Mergesort (A, N, 0, R [J]-1 );

Finish = clock ();

Duration = (finish-Start)/clocks_per_sec * 1000;

Cout <"mergesort need the time:" <duration <Endl;

// Print (A, R [J]);

 

Exchange (A, N, R [J]);

Start = clock ();

Quicksort (A, 0, R [J]-1 );

Finish = clock ();

Duration = (finish-Start)/clocks_per_sec * 1000;

Cout <"quicksort need the time:" <duration <Endl;

// Print (A, R [J]);

}

Return 0;

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zshrong/archive/2009/10/26/4730468.aspx

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.