Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program is used for Selection Sort. (C++)


This program is used for Selection Sort. (C++)
 
 
#include< stdlib.h>
#include< iostream.h>
#include< conio.h>
#define NUM_ITEMS 10
void selectionSort(int numbers[], int array_size);
 
int numbers[NUM_ITEMS];
 
int main()
{
clrscr();
int i;
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand() %100;
//display the unsorted items 
cout< < "Unsorted elements are::\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
cout< < endl;
//perform selection sort on array
selectionSort(numbers, NUM_ITEMS);
cout< < "Sorted elements are::\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
}
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
 
 

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP