ADA SELECTION SORT
ADA SELECTION SORT
BOMMANAHALLI
DEPERTMENT OF COMPUTER SCIENCE ENGINEERING
NAME:THIPPESWAMY K S(1OX23CS421)
CHANDAN M(1OX23CS021)
CLASS: 2nd year BE(4th SEM)
TOPIC: SELECTION SORT
SUBJECT: –Analysis and Design of Algorithm
SUBJECT CODE:BCS401
SUMBITTED TO:-
Prof M.RAMYA SRI MAM
INTRODUCTION:
Selection Sort is a simple and efficient sorting algorithm used to arrange elements in either
ascending or descending order. It is an in-place, comparison-based sorting technique that
works by repeatedly selecting the smallest (or largest) element from the unsorted part of
the array and placing it in its correct position. The algorithm divides the list into two parts:
the sorted and unsorted sections. The sorted section grows as the algorithm proceeds, while
the unsorted section shrinks.
Advantages of Selection Sort
• Simple to implement and easy to understand.
• Performs well with small datasets.
• Works efficiently with nearly sorted arrays.
3. Swap the smallest element with the first element of the unsorted portion.
5. Repeat the process for the remaining unsorted portion until the entire list is sorted.
CODE:
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
selectionSort(arr, n);
return 0;
}
IMPLEMENTATION
1. Include the Standard Input-Output Library
#include <stdio.h>
• This header file allows the program to use standard input/output functions like printf()
and scanf().
• Purpose: Move the boundary between the sorted and unsorted portions of the array .
Initially, assume that the first element of the unsorted part is the smallest.
• The loop starts from i + 1 and scans through the rest of the array.
• If arr[j] is smaller than arr[minIndex], update minIndex with j.
7. Swap the Minimum Element with the First Unsorted
Element temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] =
temp;
• Swap the smallest element found (arr[minIndex]) with the first unsorted element
(arr[i]).
• This moves the smallest element to its correct position in the sorted part. 8. Print
Function
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");}
• printArray() prints the elements of the array in a single line.
The for loop iterates over all elements and prints them.
•
• This moves the smallest element to its correct position in the sorted part. 8. Print
Function
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");}
• printArray() prints the elements of the array in a single line.
The for loop iterates over all elements and prints them.
• n stores the number of elements in the array. 12. Print the Original
(Unsorted) Array printf("Original array: "); printArray(arr, n);
• Calls printArray() to display the unsorted array. 13. Call the Selection
Sort Function selectionSort(arr, n);
• Sorts the array in ascending order. 14. Print the Sorted Array
printf("Sorted array: "); printArray(arr, n);
Calls printArray() again to display the sorted array.
OUTPUT:
•
Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64