Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical No. 5 Objective - : Chandigarh University Data Structure Lab (Csp-209)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

PRACTICAL NO. 5

OBJECTIVE

Write a C++ program to sort the given data using Selection Sort.

PROBLEM DEFINITION

The Selection sort algorithm is based on the idea of finding the minimum or maximum element
in an unsorted array and then putting it in its correct position in a sorted array.

ALGORITHM

ALGORITHM SELECTION_SORT (A, N)

BEGIN.

1. Set A[0]:= - infinity


2. FOR K:=1 to N-1 //No. of Passes
{
TEMP:=A[K], PTR:=K-1;
WHILE (TEMP<A[PTR]) //No. of Comparisons
{
IF TEMP<A[PTR] && PTR>=0
{
A[K]:=A[K-1];
PTR:=PTR-1;
}
A[PTR+1]:=TEMP;
}
}

END.

Prateek Chauhan 16BCS1199 1|Page


CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

FLOWCHART

Prateek Chauhan 16BCS1199 2|Page


CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

CODE

#include<iostream>

using namespace std;

int main()

int i ,j ,n ,minIndex ,temp, min, a[30]; //Declaration

cout<<"Enter the number of elements:";

cin>>n;

cout<<"\nEnter the elements\n"; //Entering the elements

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

cin>>a[i];

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

min=a[i];

minIndex=i;

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

if(min>a[j])

min=a[j];

minIndex=j;

Prateek Chauhan 16BCS1199 3|Page


CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

temp=a[i];

a[i]=a[minIndex];

a[minIndex]=temp;

//Displaying the sorted array.

cout<<"\n Sorted list is as follows \n";

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

cout<<a[i]<<" ";

cout<<"\n Amitabh Kumar \n;

cout<<\n 16BCS1181 \n";

return 0;

OUTPUT

Prateek Chauhan 16BCS1199 4|Page


CHANDIGARH UNIVERSITY DATA STRUCTURE LAB (CSP-209)

Prateek Chauhan 16BCS1199 5|Page

You might also like