Sort a Given List of Numbers Using a Selection Sort - C Data Structure

C examples for Data Structure:Sort

Description

Sort a Given List of Numbers Using a Selection Sort

Demo Code

#include<stdio.h>

int myArray[20];/*from  w  w  w  .  java 2  s  . com*/
int count = 10;

void selectSort()
{
  int i, j, k, temp, intMin;
  for(i=0; i < count-1; i++) {       /* outer for loop begins */
    intMin = myArray[i];
    k = i;
    for(j = i+1; j < count; j++) {   /* inner for loop begins */
      if(intMin > myArray[j]) {   /* if statement begins */
        intMin = myArray[j];
        k = j;
      }                              /* if statement ends */
    }                                /* inner for loop end */
    temp = myArray[i];
    myArray[i] = myArray[k];
    myArray[k] = temp;
  }                                  /* outer for loop end */
}

void main()
{
  int i;
  printf("Enter the 10 integers for selection sort separated by white spaces: \n");
  for (i=0; i < count; i++)
    scanf("%d", &myArray[i]);
  fflush(stdin);
  selectSort();
  printf("Sorted List: ");
  for(i = 0; i < count; i++)
    printf("%d ",myArray[i]);
  printf("\nThank you.\n");
}

Result


Related Tutorials