Simple selection sort (C language implementation)

Source: Internet
Author: User

The simple choice of sorting is also called the direct selection sort, the basic idea is as follows:

For a group of keywords {k1,k2,..., kn}, first select the minimum value from the K1,k2,..., kn, and if it is a KZ, swap the KZ with the K1; then select the minimum value from the K2,k3, ..., Kn, and then swap the KZ with the K2. So choose and switch n-2, n-1, select the minimum value from Kn-1 and kn the KZ swaps the KZ with the Kn-1,
The last remaining is the maximum value in the sequence, which is formed by an ordered sequence of small to large.

The algorithm has a time complexity of N (n-1)/2=0 (n2).

Implementation code:

#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 1000 #define N 8 typedef struct {
    int R[max];
int length;

}sqlist;
    void print (SqList L) {int i;
    for (i=1;i<l.length;i++) printf ("%d,", l.r[i]);
    printf ("%d", l.r[i]);
printf ("\ n");
    } void Swap (SqList *l,int i,int j) {int temp=l->r[i];
    l->r[i]=l->r[j];
l->r[j]=temp;
    } void Selectsort (SqList *l) {int i,j,min;
        for (i=1;i<l->length;i++) {min=i; for (j=i+1;j<=l->length;j++) {if (L->r[min]>l->r[j]) {min=j
            ;
        } if (i!=min) {swap (l,i,min);
    int main () {int i;
    int d[n]={1,9,2,5,8,11,6,3};
    SqList L0;
    for (i=0;i<n;i++) {l0.r[i+1]=d[i];

    } l0.length=n;
    printf ("Pre-sort: \t\t\t");
    Print (L0);
    printf ("Simple selection Sort: \t\t");
    Selectsort (&l0); PrintL0);
return 0;
 }

Results:


Related Article

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.