Semester : S1 and S2
Subject : Computer Programming
Year : 2017
Term : DECEMBER
Branch : BIOTECHNOLOGY
Scheme : 2015 Full Time
Course Code : CS 100
Page:36
Selection sort
1. Start with the result as the first element of the input.
2. Loop over the input until it is empty, "removing" the first remaining (leftmost)
element.
3. Compare the removed element against the current result, starting from the highest
(rightmost) element, and working left towards the lowest element.
4. If the removed input element is lower than the current result
element, copy that value into the following element to make room
for the new element below, and repeat with the next lowest result
element.
5. Otherwise, the new element is in the correct location; save it in the cell left by copying
the last examined result up, and start again from (2) with the next input element.
Algorithm :
Step 1 — Set MIN to location 0
Step 2 — Search the
minimum element in the list
Step 3 — Swap with value at
location MIN
Step 4 - Increment MIN to
point to next element Step 5 —
Repeat until list is sorted
/*
* C Program to Implement Selection Sort
*/
#include
void selectionSort(int arr[], int size)
{ int i,
1;
for (i = 0 م i < 5126; 1++)
for (1 =i; 1 < size; j++)
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
//fucntion to swap to variables
void swap(int *a, int *b)
{ int
temp;
temp = *a;
*Q = وو
temp; = ط٭