Summary of the sort

Source: Internet
Author: User

Sorting algorithm Stability: In the original sequence, RI=RJ, and RI before RJ, and in the sorted sequence, RI is still before RJ, it is said that the sorting algorithm is stable; otherwise called unstable.

#include <stdio.h>#include<stdlib.h>/*Eazychange 2016.06.12SortC Language*//*Insert Sort idea: Start with the first element, move forward to the correct position in i+1, the first I element default order best case N, worst case n2, stable*/voidInsertionsort_wrong (intData[],intN) {    inti =0, j =0;  for(i =1; I < n; i++)    {        intTMP = Data[i];//Record Current Value         for(j = i; j >0; j--)//traverse forward starting from current value        {            if(data[j-1] > TMP)//Remember, it's wrong to write like this! Increased complexity of time{Data[j]= data[j-1]; Data[j-1] =tmp; }        }    }}voidInsertionsort (intData[],intN) {    inti =0, j =0;  for(i =1; I < n; i++)    {        intTMP =Data[i];  for(j = i; j>0&& Data[j-1] > tmp; j--)//This step guarantees the best case time complexity of n{Data[j]= data[j-1]; } Data[j]=tmp; }}/*Choose the sorting idea: each time the smallest element is selected from the data element to be sorted, it is stored at the starting position of the sequence best case N2, worst case n2, unstable*/voidSelectsort (intData[],intN) {    inti =0, j =0; intMin_index =0;//minimum element below table    intMin_num =0;//Minimum element value     for(i =0; I < n; i++) {Min_num=Data[i];  for(j = i; J < N; j + +)//loop to find the smallest element        {            if(Data[j] <=min_num) {Min_num=Data[j]; Min_index= J;//and record the subscript}} Data[min_index]= Data[i];//Replace with first elementData[i] =Min_num; }}/*bubble Sort thought: by starting from the beginning to traverse the largest number to enlarge the end of the best case N2, worst case n2, unstable, because there is >=*/voidBubblesort (intData[],intN) {    inti =0, j =0;  for(i =0; I < n-1; i++)    {         for(j =0; J < N-1I J + +)        {            if(Data[j] >= data[j +1])            {                intTMP =Data[j]; DATA[J]= Data[j +1]; Data[j+1] =tmp; }        }    }}/*Quick Sorting ideas: the right side to find a number on the left, left to find a number on the right, and finally assign the middle value, resulting in an intermediate value sequence. Recursion again. Best case Nlogn, worst case n2, unstable*/voidQuickSort (intData[],intLeftintRight ) {    if(Left <Right ) {        inti = left, j =Right ; intTMP =Data[left];  while(I < J)//This sort of trip produces a sequence of intermediate numbers        {             while(I < J && Data[j]>tmp)//find the first number less than TMP from the right{J--; }            if(I < J)//give that number to the left count value .{data[i++] =Data[j]; }             while(I < J && Data[i] < TMP)//find the first number greater than TMP from the left{i++; }            if(I < J)//count that number to the right .{data[j--] =Data[i]; }} Data[i]= tmp;//and assign the middle value to the position of when.        /*for (int k = 0; k <; k++) {printf ("%d", data[k]); } printf ("\ n");*/QuickSort (data, left, I-1);//At this point the middle value is in the position of I, from left to i-1 again.QuickSort (data, i +1, right);//from I+1 to right, and then again.    }}intMainvoid){    intMat[] = {3,2,1,4,6, the,8,9,Ten, -, + }; //Bubblesort (Mat, sizeof (MAT)/sizeof (mat[0]));QuickSort (Mat,0,Ten); return 0;}

Attach a quick sort of time complexity analysis: http://book.51cto.com/art/201108/287089.htm

Summary of the sort

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.