ccplusplus.com
Learn C, C++ Concepts
Showing posts with label
Data Structure using C++
.
Show all posts
Showing posts with label
Data Structure using C++
.
Show all posts
Thursday, February 16, 2012
selection sort in c
/************************************************************************* * File : selection-sort.c * Author : Saurabh Gupta * Desc : selection sort 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]$ */
Friday, September 16, 2011
insertion sort in c
/****************************************************** * File : insertion-sort.c * Author : Saurabh Gupta * Desc : insertion sort c code * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 09:45 16 September 2011 * Note : *****************************************************/ #include <string.h> #include <stdio.h> #include <stdlib.h> /* * The Insertion Sort. */ void insertionSort(char *pcString, int count) { int a, b; char t; for(a=1; a < count; ++a) { t = pcString[a]; for(b=a-1; (b >= 0) && (t < pcString[b]); b--) { pcString[b+1] = pcString[b]; } pcString[b+1] = t; } } int main () { char szString[255]; printf("Enter a string to use inseryion sort : "); gets(szString); insertionSort(szString, strlen(szString)); printf("The sorted string is: %s.\n", szString); return 0; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc insertion-sort.c -o insertion-sort [sgupta@rhel54x64 c]$ ./insertion-sort Enter a string to use inseryion sort : saurabh The sorted string is: aabhrsu. [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
Older Posts
Home
Subscribe to:
Posts (Atom)