0% found this document useful (0 votes)
271 views5 pages

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

This document discusses selection sort algorithm. It provides (1) the objective to write a C++ program to sort data using selection sort, (2) defines the problem as finding the minimum or maximum element and placing it in the correct position in a sorted array, (3) outlines the selection sort algorithm pseudocode to find the minimum element and swap it with the first element in each pass. It also includes the flowchart and C++ code implementing selection sort on an array.

Uploaded by

Amitabh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views5 pages

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

This document discusses selection sort algorithm. It provides (1) the objective to write a C++ program to sort data using selection sort, (2) defines the problem as finding the minimum or maximum element and placing it in the correct position in a sorted array, (3) outlines the selection sort algorithm pseudocode to find the minimum element and swap it with the first element in each pass. It also includes the flowchart and C++ code implementing selection sort on an array.

Uploaded by

Amitabh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy