The document describes the selection sort algorithm, which sorts an array by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning. It outlines the step-by-step process of sorting a given array of integers and highlights the advantages and disadvantages of the algorithm. The selection sort is simple and effective for small datasets but inefficient for larger ones and does not maintain the relative order of equal elements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views6 pages
Selection
The document describes the selection sort algorithm, which sorts an array by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning. It outlines the step-by-step process of sorting a given array of integers and highlights the advantages and disadvantages of the algorithm. The selection sort is simple and effective for small datasets but inefficient for larger ones and does not maintain the relative order of equal elements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6
# Array to be sorted •arr is the list of integers that we want to sort.
arr = [2, 45, 0, 11, 9, 88, 747]
•size is the length of the array, which is calculated using the len() # Size of the array size = len(arr) # Selection sort algorithm function. Outer Loop: Iterating Through Each Element for ind in range(size): The outer loop runs from the first element to the last element of the min_index = ind array (ind ranges from 0 to size-1).min_index is initially set to the for j in range(ind + 1, size): current index (ind). This variable will hold the index of the smallest # Select the minimum element in every iteration element found in the unsorted portion of the array. if arr[j] < arr[min_index]: min_index = j # Swapping the elements to sort the array Inner Loop: Finding the Minimum Element •The inner loop starts from the element right after the current arr[ind], arr[min_index] = arr[min_index], arr[ind] # Printing the sorted array element (ind + 1) and goes till the end of the array (size). •It checks each element to see if it is smaller than the current print(‘Sorting in Ascending Order by selection sort is:') print(arr) minimum element (arr[min_index]). •If a smaller element is found, min_index is updated to the index of this new minimum element (j).
Swapping the elements to sort the array
•After the inner loop has found the minimum element in the unsorted portion of the array, we swap this minimum element with the element at the current index (ind). •This effectively places the minimum element in its correct sorted position. The sorting process step-by-step for the given array [2, 45, 0, 11, 9, 88, 747]: 1.Initial array: [2, 45, 0, 11, 9, 88, 747] 2.First pass (ind = 0): •min_index starts at 0 •Inner loop finds 0 at index 2 •Swap 2 with 0 •Array after first pass: [0, 45, 2, 11, 9, 88, 747]
3.Second pass (ind = 1):
•min_index starts at 1 •Inner loop finds 2 at index 2 •Swap 45 with 2 •Array after second pass: [0, 2, 45, 11, 9, 88, 747]
4.Third pass (ind = 2):
•min_index starts at 2 •Inner loop finds 9 at index 4 •Swap 45 with 9 •Array after third pass: [0, 2, 9, 11, 45, 88, 747] 5.Fourth pass (ind = 3): •min_index starts at 3 •No smaller element found •Array remains the same: [0, 2, 9, 11, 45, 88, 747] 6.Fifth pass (ind = 4): •min_index starts at 4 •No smaller element found •Array remains the same: `[0, 2, 9, 11, •Initial array: [2, 45, 0, 11, 9, 88, 747] •First pass (ind = 0): •min_index starts at 0 •Inner loop finds 0 at index 2 •Swap 2 with 0 •Array after first pass: [0, 45, 2, 11, 9, 88, 747] •Second pass (ind = 1): •min_index starts at 1 •Inner loop finds 2 at index 2 •Swap 45 with 2 •Array after second pass: [0, 2, 45, 11, 9, 88, 747] •Third pass (ind = 2): •min_index starts at 2 •Inner loop finds 9 at index 4 •Swap 45 with 9 •Array after third pass: [0, 2, 9, 11, 45, 88, 747] •Fourth pass (ind = 3): •min_index starts at 3 •No smaller element found •Array remains the same: [0, 2, 9, 11, 45, 88, 747] •Fifth pass (ind = 4): •min_index starts at 4 •No smaller element found •Array remains the same: [0, 2, 9, 11, 45, 88, 747] •Sixth pass (ind = 5): •min_index starts at 5 •No smaller element found •Array remains the same: [0, 2, 9, 11, 45, 88, 747] •Seventh pass (ind = 6): •min_index starts at 6 •No smaller element found •Array remains the same: [0, 2, 9, 11, 45, 88, 747] Selection Sort Algorithm • The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning. • Start with the first element of the array as the initial "sorted" part. • Find the smallest element in the remaining unsorted part. • Swap this smallest element with the first element of the unsorted part. • Move the boundary between the sorted and unsorted parts one element to the right. • Repeat steps 2-4 until the entire array is sorted.
Advantages of Selection Sort Algorithm
•Simple and easy to understand. •Works well with small datasets.
Disadvantages of the Selection Sort Algorithm
•Does not work well on large datasets. •Does not preserve the relative order of items with equal keys which means it is n stable Array as an example: arr[] = {64, 25, 12, 22, 11}