Simple selection and sorting of C language data structure

Source: Internet
Author: User

Algorithm: Sets the number of records for the sorted sequence to n. I take the,..., n-1, and from All N-i+1 records (ri,ri+1,..., Rn) to find the smallest record of the sort code, exchange with the first record. The sequencing of the record sequence is completed after the n-1 is executed.

Compiler: VS2013

Code

#include "stdafx.h"
#include <stdlib.h>

function declaration

void Selectsort (int a[], int n); //Simple selection sort (small to large)

int main ()
{
int i,n,a[100];
printf ("Please enter the number of elements that need to be sorted:");
scanf_s ("%d", &n);
printf ("Randomly generated array is:");
for (i = 1; I <= n; i++)
{
A[i] = rand ()% 100 + 1;
printf ("%d", a[i]);
}
A[i] = ' + ';
printf ("\ n");

Selectsort (A,n);

}

Simple selection sort (small to large)
void Selectsort (int a[],int N)
{
int I=1, J, K,min;
for (min = a[1]; I <= n; i++)
{
K = i;
for (j = i; J <= N; j + +)
if (A[j] < a[k])
K = J;
if (k! = i)
{
min = a[k];
A[K] = A[i];
A[i] = min;
}
}
printf ("\ n Simple selection of results after sorting (from small to Large):");
for (i = 1; I <= n; i++)
printf ("%d", a[i]);
printf ("\ n");
}

Results

Simple selection and sorting of C language data structure

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.