Sunday, November 7, 2010

Computer Science: 5th sem ADA Lab Program 4

Analysis and Design of Algorithms Lab.


Question 4



//Sort a given set of elements using selection sort and determine the time required to sort the given set of elements

#include <iostream>

#include <conio.h>

#include <time.h>


using namespace std;


void selectionSort(int *arr,int n)

{

for(int i=0;i<n-1;i++)

{

int min=i;

for(int j=i+1;j<n;j++)

if(arr[min]>arr[j])

min=j;

int tmp=arr[min];

arr[min]=arr[i];

arr[i]=tmp;

}

}


void main()

{

int n;

cout<<"Enter the size of the array:";cin>>n;

cout<<"Enter the array of size n in ascending order:";

int *arr=new int[n];

for(int i=0;i<n;i++)

cin>>arr[i];

int start=clock();

selectionSort(arr,n);

int time1=(clock()-start)/CLK_TCK;



cout<<"\nSorted Array:\n";

for(int i=0;i<n;i++)

cout<<arr[i]<<"\t";

getch();

}


No comments:

Post a Comment