ccplusplus.com
Learn C, C++ Concepts
Thursday, February 16, 2012
selection sort algorithm in c
/************************************************************************* * File : selection-sort.c * Author : Saurabh Gupta * Desc : selection sort algorithm in c * Source : http://ccplusplus.com/p/c.html * Created : 10:54 AM Thursday, February 16, 2012 *************************************************************************/ #include
const int NUM_SIZE = 8; void selectionSort(int anUserNum[], int n) { int i, j; int minIndex, tmp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) { if (anUserNum[j] < anUserNum[minIndex]) { minIndex = j; } } if (minIndex != i) { tmp = anUserNum[i]; anUserNum[i] = anUserNum[minIndex]; anUserNum[minIndex] = tmp; } } } void takeUserInput (int anUserNum []) { int nNum; int i = 0; printf ("Enter %d number to perform Selection Sort\n", NUM_SIZE); for (; i < NUM_SIZE; i++) { scanf ("%d", &nNum); anUserNum [i] = nNum; } } void displayNumberAgaian (int anUserNum []) { int j = 0; for (; j < NUM_SIZE; j++) { printf ("%d\t", anUserNum[j]); } printf ("\n"); } int main () { int anUserNum [NUM_SIZE]; takeUserInput (anUserNum); printf ("Performing Selection Sort on the following Number :\n"); displayNumberAgaian (anUserNum); selectionSort (anUserNum, NUM_SIZE); printf ("Numbers after Selection Sort :\n"); displayNumberAgaian (anUserNum); return 0; } /* * * [sgupta@rhel6x86 data-structure]$ gcc selection-sort.c -o selection-sort [sgupta@rhel6x86 data-structure]$ ./selection-sort Enter 8 number to perform Selection Sort 5 1 12 -5 16 2 12 14 performing Selection Sort on the following numbers : 5 1 12 -5 16 2 12 14 Numbers after Selection Sort : -5 1 2 5 12 12 14 16 [sgupta@rhel6x86 data-structure]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment