Comparison of 6 kinds of internal sorting algorithms for data structure

Source: Internet
Author: User
Tags sort

1. Demand Analysis

(1) The input data in the form of pseudo-random number generation program, and each input number of not less than 100, at least 5 different sets of input data

(2) The output is as follows: The number of times the OUTPUT keyword participates and the number of times the keyword was moved (the keyword Exchange is 3 moves)

(3) The program can achieve the function: on the bubble sorting, direct insertion sort, simple selection of sorting, quick sorting, hill sort, heap sorting these 6 commonly used internal sorting algorithm to compare, compared with the key words to participate in the comparison of the number of keywords and the number of moves (keyword Exchange is 3 moves)

(4) test data: The correct input is generated by the pseudo-random number of the program generates 100 random numbers, and then output the comparison results, the error input is a very small amount of data, when the output can not be compared

(5) C language writing

2. Outline Design

This program designed a sequential table structure to store the data that needs to be sorted, after the main program is run, initialize 6 sequential tables, then enter a cycle that needs to be cycled 5 times, there is a small loop inside the loop that needs to execute 100 times to produce 100 random numbers to 6 sequential tables, At this point the data for these 6 sequential tables is the same and is random, then the void Insertsort (SqList *l) bubble sort is called, void Insertsort (SqList *l) is inserted directly into the sort, void Selectsort (SqList *l) Simple select sort, void QuickSort (sqlist *l) Quick sort, void Shellsort (SqList *l) hill sort, void Heapsort (SqList *l) heap sort, to treat sort order. where void Insertsort (SqList *l), void Selectsort (SqList *l), void Heapsort (SqList *l) are in these modules, void swap is called when two data is required to swap locations ( SqList *l,int i,int J) module. void Heapadjust (sqlist *l,int s,int m,int &a,int &b) was called in the Heapsort (sqlist *l) module, allowing L->r[1...i-1] to be re-resized to a large top heap. void QuickSort (sqlist *l) module called void QSort (sqlist *l,int low,int high,int &k,int &l) to pair the sub-sequences in the order table L L->r[low: High] As a quick sort, void QSort (sqlist *l,int low,int high,int &k,int &l) called int Partition (sqlist *l,int low,int high,int &k,int &l) to be l->r[low. High] in Split, the pivot value is calculated.

3. Detailed design

The sequential table structure has an array of type data stored for int, r[0] used as a sentinel or temporary variable, and a length variable of type int for the record order table.

typedef struct{

int r[maxsize+1];

int length;

}sqlist;

4. Commissioning Analysis

(1) Problems encountered during the commissioning process:

1, at the beginning of the random number, the array of sequential tables are assigned values, the results come out, the first number will not be sorted, the other number is normal, because r[0] is used as sentinel or storage temporary variables, so at the beginning of the assignment, r[0] should not be assigned to a value.

2. When calculating the number of keyword exchanges, the variable defined is int l; After the calculation results, the number is very large. is because for local variables, do not assign the initial value, in fact, it is stored in a random values, and not 0, so the definition should be defined as int l = 0;

3, in the calculation of the comparison and exchange of keywords, because the module to call each other, so the calculated value as a parameter transfer, but return can not return two return value, troubled for a long time, and then use the int &k,int &l, that can change the method of the argument to solve the problem

(2) Spatio-temporal analysis of the algorithm:

Sorting method Average Time worst case stability extra space notes

Bubbling O (n2) O (n2) stabilized O (1) n-hour better

Select O (n2) O (n2) unstable O (1) n-hour better

Insert O (NLOGN) O (n2) stable O (1) Most of them are sorted better

Hill O (n^1.5) Unknown unstable O (1)

Fast O (Nlogn) O (n2) unstable O (NLOGN) n Good

Heap O (Nlogn) O (nlogn) unstable O (1) n Large time better

Bubble Sort Optimization: When the sequence has not yet been compared but is already in order, in fact has not continued the back of the loop to judge the work, so add a scalar flag to achieve the improvement of the algorithm

void BubbleSort2 (SqList *l) {

int i,j;

Status flag = TRUE;

for (i = 1;i<l->length && flag;i++) {

flag = FALSE;

for (j = l->length;j>=i;j--) {

if (L->r[j] > l->r[j+1]) {

Swap (l,j,j+1);

flag = TRUE;

}

}

}

}

(3) Experience: Before starting to play the code to adjust the idea, find the best entrance to write code, the call between the modules should be first conceived, otherwise the code is too confusing, when there is a bug, it will be difficult to find, to make full use of debugging functions

5. user's usage instructions

It is not possible for the user to enter 100 numbers in this program. Therefore, the program will automatically generate random numbers after execution. Direct comparison results are automatically displayed. Simple to use, run directly on the line.

6. Test Results

The data from the test can see that the bubble sort and the keywords in the simple sort have the same number of comparisons and do not fluctuate, because regardless of the number of data changes, as long as the total is the same, both the bubbling sort and the simple sort execute the comparison statements in the loop. The key words in the simple sort have the least moving readme and the fluctuation amplitude is small, because the direct insertion sort is inserting records from the unordered area directly into the ordered area, so there is no exchange between the data, so the number of moves is less, but the number of comparisons is more. The difference between the number of comparisons and the number of moves in a heap sort is due to the fact that the maximum value is often compared to the end in the heap ordering and then exchanged. The number of keywords in the hill sort and fast-moving row fluctuates significantly. Because the two sorts of data exchange between the position of the action is large, so when the data is more chaotic and more orderly, the results of the number of moves will be larger.

5. Appendix

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 100

Sequential table structure for sorting

typedef struct{

The int r[maxsize+1];//is used to store the array to sort, r[0] as Sentinel or temporary variable

int length;

}sqlist;

Exchange two values

void swap (SqList *l,int i,int j) {

int temp = l->r[i];

L->r[i] = l->r[j];

L->R[J] = temp;

}

Bubble sort

void Bubblesort (SqList *l) {

int i,j,k=0,l=0;

for (i = 1;i<l->length;i++) {//Outer loop, make sure all numbers are compared to other numbers

k++;

for (j = i+1;j<=l->length;j++) {//inner loop, compare size with one number to other

k++;

if (L->r[i] > L->r[j]) {

Swap (L,I,J);

L = l+3;

}

}

}

printf ("The number of comparisons for keywords in bubble sort is%d:", k);

printf ("\ n the number of moves of the keyword in the bubble sort is%d:", l);

printf ("\ n");

}

Direct sort

void Insertsort (SqList *l) {

int i,j,k=0,l=0;

for (i = 2;i<=l->length;i++) {

k++;

if (L->r[i] < l->r[i-1]) {

L->r[0] = l->r[i];//Set Sentinel

l++;

for (j = i-1; L->R[J] > l->r[0];j--) {

L->R[J+1] = l->r[j];//record and move back

l++;

k++;

}

k++;//This step is easy to ignore, jump out of the loop, is compared once, does not meet the criteria to jump out of

L->R[J+1] = l->r[0];//inserted into the correct position

l++;

}

}

printf ("The number of comparisons for keywords in direct sorting is%d:", k);

printf ("\ n" the number of moves of the keyword in direct order is%d: ", l);

printf ("\ n");

}

Simple selection sorting

void Selectsort (SqList *l) {

int i,j,min;

int k=0,l=0;

for (i = 1;i<l->length;i++) {

k++;

min = i;

for (j = i+1;j<=l->length;j++) {

k++;

if (L->r[min] > L->r[j]) {

min = j;

}

}

if (i! = min) {//Judge I!min, it proves that the data is smaller than r[min], you need to exchange

Swap (l,i,min);

L = l+3;

}

}

printf ("keywords in simple sort are compared by:%d", k);

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.