Preliminary discussion on several sorting algorithms

Source: Internet
Author: User

                                                                                     

Summary of multiple sorting algorithms (not including detailed projections of complexity)

Stable sorting and unstable ordering

Stable ordering: The relative position of the same element in the sort does not change.

Unstable sort: The relative position of the same element in the sort is changed.

Internal sorting and external sorting:

Internal sorting: The sorting process for rows of records and content placed in the computer's random memory

External sorting: Generally refers to the number of records to be sorted is very large, so that in memory can not fully accommodate the entire record, during the sorting process, the need for external access to the sorting process.

The complexity of the sorting algorithm:

Here we generally only consider the complexity of the time, of course, the individual sort of spatial complexity is also very large, such as Hill sort. The computational method of time complexity is similar to that of general complexity.

The differences that are based on the sorting process can be categorized as:

Insert sort, swap sort, select sort, merge sort, base sort, binary sort tree sort


Insert Sort:

Sorting by inserting operations on an ordered table

Insertion of an ordered table: Inserts a record into an ordered table that has been arranged so that a new ordered table is obtained.

The direct insertion sort algorithm mainly applies the comparison and the movement two kinds of operation.

void Insertsort (int r[],int n) {int i,j,temp;for (i=1;i<n;i++) {temp=r[i];j=i-1;while (j>=0) && (temp< R[j]) {r[j+1]=r[j];j--;} R[j+1]=temp;//put single on First}</span>
                   

Hill sort (an improvement with a direct insert sort)

Methods: The sequential sequence was divided into several sub-sequences, and then the direct insertion sort was performed, and a direct insertion sort was performed once the records in the whole sequence were basically ordered.

The time complexity of hill sequencing:

O (nlog2n) and O (n squared) approximately O (1.3 times N)

               
void Shellsort (int a[],int arrsize) {int Temp;int gap=arrsize/2;while (Gap! = 0) {for (int i=gap;i<arrsize;i++) {temp=a[ i];for (int j=i;j>=gap;j-=gap) {if (temp < a[j-gap]) A[j]=a[j-gap];elsebreak;} A[J] = temp;} Gap = (int) (GAP/2);}} </span>


Swap sort--bubble sort

Thought: By continuously comparing the size of adjacent elements, the exchange implementation of sorting

void Bubblesort (int a[],int n) {int flag = 1;for (i=1;i<n;i++) {flag=0;for (int j=n-1;j>=i;j--) if (r[j]<r[j-1]) {T  = R[j]; R[J]=R[J-1]; R[j-1]=t;flag=1;} if (flag==0) return;}} </span>

Exchange sort--quick sort

An improved algorithm for bubble sorting

Thought: With the first record as the Axis record, the former, the back scan sequence, through the exchange, realizes the big value record to move, the small value record moves forward, finally places the axis record in an appropriate position (the small value records before, the large value records after)

The axis record divides the original sequence into two parts, then re-sets the axis records for the front and back parts, and then makes a quick order respectively. Until the entire sequence is ordered.

Quick Sort----segmentation process

Fast sorting is a divide-and-conquer algorithm

For a sequence, take one of its elements as an axis, place all the elements of the small and the axis on its left, and the elements greater than it on the right side of it.

Segmentation algorithm:

To back up an axis with temporary variables

Take the chain of the pointer low and Hing, their initial value is the sequence of the two ends of the table, in the whole process to ensure that less than high

Move two pointers

First, from the location known to the Hing to the left, find the first small and the axis of the element, put this element in the position of low

From low start to right, find the first large and axis element, put it in the Hing position

Repeat the process until Low=high

Place the shaft in the position indicated by low

In this way all elements greater than the axis are north on the right, and all small and axis elements are placed on the left.

void Fquick_sort (int *addr,int low,int high) {int lowpos = Low;int highpos= high;int povite = addr[low];if (Lowpos >= hig Hpos) {return;} while (Lowpos < Highpos) {while ((Lowpos < Highpos) && (add[highpos]>=povite)) {highpos--;} if (Lowpos < Highpos) {Addr[lowpos] = addr[highpos];lowpos++;} while ((Lowpos < Highpos) && (addr[lowpos]<=povite)) {lowpos++;} if (Lowpos < Highpos) {addr[highpos]=addr[lowpos];highpos--;}} Addr[lowpos]=povit; Fquick_sort (ADDR,LOW,LOWPOS-1); Fquick_sort (Addr,lowpos+1,high); return;} </span>

Time complexity: Average O (nlog2n)

Space complexity: O (n squared)

Fast sorting is an unstable sort method

 

Simple selection Sort:

Each trip selects a maximum or minimum element and places it in the appropriate position.

void Selsectsort (int a[],int n) {int i,j;int temp;for (i=0;i<n-1;i++) for (j=i+1;j<n;j++) {if (A[i]>a[j]) {temp=a [I];a[i]=a[j];a[j]=a[i];}} </span>

Merge sort:

Thought: Merge two ordered sequences into an ordered sequence, so if there are only two elements, the merge sort is to break up a sequence until it is split into one element, and then merge sorts.

void merge (int *add,int len) {int LenA = 0,lenb = 0,lenc = 0,lentmp = 0;int *ARRC = null;if (len <= 1) {return;} ArrC = (int *) malloc (len*sizeof (int)); lentmp = Len/2;merge (add,lentmp); merge (&add[lentmp],len-lentmp); LenA = 0; LenB = Lentmp;lenc = 0;while ((LenA! = lentmp) && (LenB! = len)) {if (Add[lena]<add[lenb]) {Arrc[lenc] = Add[lena]; lena++;} Else{arrc[lenc] = add[lenb];lenb++;} lenc++;} if (LenA = = lentemp) {while (LenB < Len) {arrc[lenc++]=add[lenb++];}} else if (LenB = = len) {while (LenA < lentmp) {arrc[lenc++]=add[lena++];}} else{//printf ("All OK!\n"), memcpy (add,arrc,len*sizeof (int)); free (ArrC);</span>





Preliminary discussion on several sorting algorithms

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.