Example of a "data structure" selection sorting algorithm

Source: Internet
Author: User

The basic choice sort edit sorting algorithm is the algorithm that solves the following problem: the sequence of input n number <a1,a2,a3,..., an>. Output the original sequence of a reflow <a1*,a2*,a3*,..., an*>;, so that the a1*<=a2*<=a3*<=...<=an* sorting algorithm has a lot of, including insert sort, bubble sort, heap sort, merge sort, select Sort, Counting sort, cardinal sort, bucket sort, quick sort etc. Insert sort, heap sort, select sort, merge sort and quick sort, bubble sort is comparison sort, they are sorted by comparing elements in array, other sort algorithm uses non-comparison method to get sort information about input array. Thought n the direct selection sort of the files of the records can be ordered by N-1 Direct selection sort to get an orderly result: ① Initial state: Unordered area is R[1..N], ordered area is empty. ② the 1th order in the unordered area R[1..N] to select the smallest key record r[k], it and the 1th record of the unordered zone r[1] exchange, so that r[1..1] and R[2..N] respectively to increase the number of records added a new ordered area and the number of records reduced by 1 new unordered area. ...... ③ I-trip sequencing the first-order sequence begins, the current ordered and unordered areas are r[1..i-1] and R (I.) respectively. N). The sequencing selects the smallest record of the keyword from the current unordered area R[k], swapping it with the 1th record R in the unordered zone, so that r[1..i] and R change to a new unordered area with a new ordered area and a number of records with a decrease of 1 respectively. The popular explanation contrasts with the size of the previous element in the array, and if the following element is smaller than the previous element, use a variable K to remember his position, and then the second comparison, the front "last element" now becomes "the previous element" and continues with his "latter element" To compare if the next element is smaller than he would use the variable K to remember its position in the array (subscript), and when the loop ends, we should find the lowest number of the subscript, and then to judge, if the index of the element is not the first element subscript, let the first element to exchange values with him, This will find the smallest number in the entire array. Then find the second small number in the array, let him swap the value with the second element in the array, and so on. Code implementation:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 #include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>#define SIZE 15usingnamespacestd;voidselectSort(int*a, intlen){    inth;    inttmp;    for(int i=0; i<SIZE-1; i++){        intk=i;        for(intj=i+1; j<len; j++){            if(a[j]<a[k])            k=j;        }        if(k!=i){            tmp=a[i];            a[i]=a[k];            a[k]=tmp;        }        cout<<"第"<<i<<"步排序结果为:";        for(inth=0; h<len ; h++){            cout<<a[h]<<" ";        }        cout<<endl;    }}intmain(){    intarray[SIZE];    srand(time(NULL));    for(inti=0 ;i<SIZE; i++){        array[i]=rand()/1000+100;    }      cout<<"排序前的序列为:"<<endl;    for(inti=0; i<SIZE; i++){        cout<<array[i]<<" ";    }    cout<<endl<<endl;    selectSort(array, SIZE);    cout<<endl;    cout<<"排序后的序列为:"<<endl;    for(inti=0; i<SIZE; i++){        cout<<array[i]<<" ";    }    return0;}

Operation Result:

Example of a "data structure" selection sorting algorithm

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.