0% found this document useful (0 votes)
20 views13 pages

ADA SELECTION SORT

The document provides an overview of the Selection Sort algorithm, explaining its mechanism of sorting elements by repeatedly selecting the smallest or largest element from the unsorted portion and placing it in the correct position. It discusses the advantages and disadvantages of Selection Sort, including its efficiency with small datasets and its O(n²) time complexity for larger datasets. Additionally, the document includes a step-by-step example of the algorithm in action and provides a code implementation in C.

Uploaded by

thippeshks767
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)
20 views13 pages

ADA SELECTION SORT

The document provides an overview of the Selection Sort algorithm, explaining its mechanism of sorting elements by repeatedly selecting the smallest or largest element from the unsorted portion and placing it in the correct position. It discusses the advantages and disadvantages of Selection Sort, including its efficiency with small datasets and its O(n²) time complexity for larger datasets. Additionally, the document includes a step-by-step example of the algorithm in action and provides a code implementation in C.

Uploaded by

thippeshks767
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/ 13

THE OXFORD COLLAGEOFENGINEERING

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.

Disadvantages of Selection Sort


• Inefficient for large datasets due to its O(n²) complexity.
• It does not adapt to the input order, meaning it performs the same number of
comparisons regardless of initial arrangement.
Example of Selection Sort
Consider the following array: [64, 25, 12, 22, 11]
• Find the minimum element (11) and swap it with the first element (64): [11, 25, 12, 22,
64]
• Find the minimum element (12) from the remaining unsorted part and swap it with 25:
[11, 12, 25, 22, 64]
• Find the minimum element (22) and swap it with 25: [11, 12, 22, 25, 64]

• nested loops to find The array is now sorted.

Time and Space Complexity


Selection Sort has a time complexity of O(n²) in all cases (best, worst, and average) because it
requires the minimum element. However, it is efficient for small datasets. The space
complexity is O(1) since it does not require additional memory.

Case Time Complexity

Best Case (Already Sorted) O(n²)

Average Case O(n²)

Worst Case (Reverse Sorted) O(n²)


SELECTION SORT ALGORITHM
Selection Sort is a straightforward comparison-based sorting technique. It works by repeatedly
selecting the smallest (or largest, depending on the order) element from the unsorted part of
the list and placing it at the beginning.

Steps of Selection Sort Algorithm


1. Start with the first element as the minimum.

2. Scan the rest of the array to find the smallest element.

3. Swap the smallest element with the first element of the unsorted portion.

4. Move the boundary of the sorted portion one step forward.

5. Repeat the process for the remaining unsorted portion until the entire list is sorted.

Procedure of Selection Sort:


procedure SelectionSort(array, n)
for i = 0 to n-1 do
minIndex = i // Assume the current index has the
smallest value
for j = i+1 to n-1 do // Search the rest of the array
if array[j] < array[minIndex] then
minIndex = j // Update minIndex if a smaller value is
found
end for
if minIndex != i then // Swap only if a new minimum is found
swap(array[i], array[minIndex]) end if end for
end procedure

EXAMPLE OF SELECTION SORT


Unsorted Array:
64,25,12,22,11 Step-by-step
Execution:
1. First Pass:
o Find the smallest element in 64,25,12,22,11 → 11 o
Swap 11 with 64
o Array after swap: 11,25,12,22,64
2. Second Pass:
o Find the smallest element in 11,25,12,22,64 → 12 o
Swap 12 with 25
o Array after swap: 11,12,25,22,64
3. Third Pass:
o Find the smallest element in 25 ,22,64 → 22 o
Swap 22 with 25
o Array after swap: 11,12,22,25,64
4. Fourth Pass:
o Find the smallest element in 25 ,64 → 25 (Already
in place) o No swap needed.
5. Sorted Array: 11,12,22,25,64

CODE:
#include <stdio.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;

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


// Assume the first element of the unsorted part as the
minimum
minIndex = i;

// Find the minimum element in the remaining unsorted array


for (j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// Swap the found minimum element with the first element of


the unsorted part temp = arr[minIndex]; arr[minIndex] =
arr[i]; arr[i] = temp;
}
}
// Function to print an array void
printArray(int arr[], int n) { for
(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Main function int main() { int
arr[] = {64, 25, 12, 22, 11}; int n
= sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(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().

2. Selection Sort Function void selectionSort(int arr[], int n) {


• void selectionSort(int arr[], int n): This function takes two parameters:
o arr[]: The array to be sorted.
o n: The number of elements in the array.
3. Variable Declarations int i, j, minIndex, temp;
• i → Outer loop variable (used to traverse the array).
• j → Inner loop variable (used to find the minimum element).
• minIndex → Stores the index of the minimum element found.

• temp → Temporary variable for swapping elements.

4. Outer Loop – Iterates Through Each Element


for (i = 0; i < n - 1; i++) {
• Runs from i = 0 to i = n-2 (since the last element will already be sorted).

• Purpose: Move the boundary between the sorted and unsorted portions of the array .

5. Assume the First Unsorted Element is the Minimum


minIndex =

Initially, assume that the first element of the unsorted part is the smallest.

6. Inner Loop – Find the Smallest Element in Unsorted Parfor (j


= i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex
= j;
}}
• Purpose: Find the minimum element in the unsorted part of the array.

• 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.

8. Inner Loop – Find the Smallest Element in Unsorted Parfor (j


= i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex
= j;
}}
• Purpose: Find the minimum element in the unsorted part of the array.
• 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.

9. 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.

9. Main Function int main() {


• The starting point of the program.

10. Declare and Initialize the Array


int arr[] = {64, 25, 12, 22, 11};
• The unsorted array we want to sort.

11. Calculate the Number of


Elements in the Array int n =
sizeof(arr) / sizeof(arr[0]);
• sizeof(arr): Total size of the array in bytes.
• sizeof(arr[0]): Size of a single element.

• 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.

15. Return 0 return


0;
Indicates successful program execution.

OUTPUT:

Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64

You might also like