0% found this document useful (0 votes)
12 views18 pages

Dsa Selection Sort

Uploaded by

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

Dsa Selection Sort

Uploaded by

Jona Lyne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DSA Selection Sort

Sorting Algorithms
1 Definition

2 Basis and Steps

3 ADT Specification

CONTENTS 4 ADT Representation

5 ADT Implementation
1 Definition

Selection Sort
It is a simple sorting algorithm. This is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part
at the left end and the unsorted part at the right end. Initially, the sorted
part is empty and the unsorted part is the entire list.

An algorithm that selects the smallest element from an unsorted list


in each iteration and places that element at the beginning of the unsorted
list.
2 Basis
Create an array to store the elements to be sort.
0 1 2 3 4 ...n

Example:
INPUT: UNSORTED ARRAY OUTPUT: SORTED ARRAY
Arr [5] = {20, 12, 10, 15, 2} Arr [5] = {2, 5, 6, 7, 11}

0 1 2 3 4 0 1 2 3 4
20 12 10 15 2 2 10 12 15 20
4 Steps

ALGORITHMS:

Step 1 − Set MIN to index 0.


Step 2 − Search the minimum element in the unsorted list.
Step 3 − Swap with the value at location MIN.
Step 4 − Increment MIN to move to next element of the array.
Step 5 − Repeat until list is sorted.
3 ADT Specification
DATA:
Array - stores the user input numbers as its elements.
int MxSize - user input array size

OPERATION:
Selection Sort - a user-defined function to sort the random
ordered elements of array in ascending or descendingorder.
4 ADT Representation
PSEUDOCODE: arr : array of items n : size of list
for i = 1 to n - 1
min = i /* set current element as minimum*/

/* check the element to be minimum */


for j = i+1 to n
if arr[j] < arr[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap arr[min] and arr[i]
end if
end for

end procedure
5 ADT Implementation
The following implements the selection sort through a user-defined
function.
void selectionSort(int arr[], int Mxsize)
{
for (int i = 0; i < Mxsize-1; i++) { if (min != i) {
int min = i; //sets min to index i [0] int temp = arr[min];
//iterate the elements arr[min] = arr[i];
for (int j = i + 1; j < Mxsize; j++) arr[i] = temp;
{ }
if (arr[j] < arr[min]) { //compare elements to minimum }
min = j; //sets the smallest number j in array to min. }
}
}
5 ADT Implementation

void selectionSort(int arr[], int Mxsize)


{
for (int i = 0; i < Mxsize-1; i++) {
int min = i; //sets min to index i [0]

Using for loop, the evaluation of the array's elements is


through increment with its index 0 as starting point and also
set to be the first minimum element.
5 ADT Implementation
void selectionSort cont'd...

//iterate the elements


for (int j = i + 1; j < Mxsize; j++)
{
if (arr[j] > arr[min]) { //compare elements to minimum
min = j; //sets the element j in array as new min.
}
}
5 ADT Implementation

Search for the smallest


element in the array and
compare it to the current
minimum. Swap the
position of the smallest
found element to the
current minimum.
5 ADT Implementation

if (min != i) {
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}

Checks if the minimum is not same as the element's value in


the current index. Then, perform swapping of position with the
lower element placed in left side of the array.
5 ADT Implementation

If the current element was found to be in right order, move to


next element and perform same steps of comparison and
swapping.
5 ADT Implementation
The following prompt the user to input the array size and elements to store.
int Mxsize =0; //determine the array size

cout << "****A program that will arrange the numbers in ascending order using
SELECTION SORT.****\n\n";
cout << "Enter how many numbers will be sorted: ";
cin >> Mxsize;
cout << "\nEnter " <<Mxsize <<" numbers in random order: " << endl;

//prompt user to enter inputs and store it in array


int myarr[Mxsize];
for (int i = 0; i < Mxsize; i++) {
cin >> myarr[i];
}
5 ADT Implementation
The following for-loops displayed the unsorted first followed by the calling
of selection sort function in order to display the sorted array.

// function call
selectionSort(myarr,Mxsize);
//display unsorted array
cout << "UNSORTED ARRAY: " << endl; //display sorted array in ascending order
for (int i = 0; i < Mxsize; i++) { cout << "SORTED ARRAY: " << endl;
cout << myarr[i] << "\t"; for (int i = 0; i < Mxsize; i++) {
} cout << myarr[i] << "\t";
}
5 ADT Implementation
When the program was executed, it prints the output in ASCENDING:
5 ADT Implementation
When the program was executed, it prints the output in DESCENDING:
Thanks!

You might also like