Notification texts go here Contact Us Click here

Selection sort in Data Structure with program

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated


 The selection sort algorithm is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted portion of the list and swaps it with the element in the correct position in the sorted portion. Here's a step-by-step description of how the selection sort algorithm works:

* ALGORITHM'S :

1. Start with an unsorted list of elements.

2. Find the minimum element in the unsorted portion of the list.

3. Swap the minimum element with the first element in the unsorted portion.

4. Move the boundary between the sorted and unsorted portions one element to the right.

5. Repeat steps 2-4 until the entire list is sorted.


Here's a visual representation of the selection sort algorithm on an example list `[64, 25, 12, 22, 11]`:


```

[64, 25, 12, 22, 11]   // Initial unsorted list


[11, 25, 12, 22, 64]   // Swap 11 (minimum) with 64


[11, 12, 25, 22, 64]   // Swap 12 (minimum) with 25


[11, 12, 22, 25, 64]   // Swap 22 (minimum) with 12


[11, 12, 22, 25, 64]   // Swap 25 (minimum) with 25 (no change)


[11, 12, 22, 25, 64]   // The entire list is sorted

```


At each iteration, the smallest element from the unsorted portion is selected and swapped with the appropriate position in the sorted portion. This process is repeated until the entire list is sorted.


Selection sort has an average and worst-case time complexity of O(n^2), where n is the number of elements in the list. It is an in-place sorting algorithm, meaning it doesn't require additional memory for sorting. However, it is not the most efficient sorting algorithm for large lists and is generally used for educational purposes or when the list size is small.

1. Program of  Selection sorting using C language .

  1. #include <stdio.h>
  2.  
  3. void selectionSort(int arr[], int n) {
  4. int i, j, minIndex, temp;
  5.  
  6. // Traverse through all array elements
  7. for (i = 0; i < n - 1; i++) {
  8. // Find the minimum element in the unsorted portion
  9. minIndex = i;
  10. for (j = i + 1; j < n; j++) {
  11. if (arr[j] < arr[minIndex])
  12. minIndex = j;
  13. }
  14.  
  15. // Swap the found minimum element with the first element
  16. temp = arr[i];
  17. arr[i] = arr[minIndex];
  18. arr[minIndex] = temp;
  19. }
  20. }
  21.  
  22. int main() {
  23. int arr[] = {64, 25, 12, 22, 11, 45, 78, 90}; // user input any number of list .
  24. int n = sizeof(arr) / sizeof(arr[0]);
  25. int i;
  26.  
  27. printf("Unsorted array: ");
  28. for (i = 0; i < n; i++) {
  29. printf("%d ", arr[i]);
  30. }
  31. printf("\n");
  32.  
  33. selectionSort(arr, n);
  34.  
  35. printf("Sorted array: ");
  36. for (i = 0; i < n; i++) {
  37. printf("%d ", arr[i]);
  38. }
  39. printf("\n");
  40.  
  41. return 0;
  42. }
  43.  
  44. OUTPUT :

  45.  
  46. Unsorted array: 64 25 12 22 11 45 78 90
  47. Sorted array: 11 12 22 25 45 64 78 90
  48.  


2. Program of  Selection sorting using Python language .

  1. ]def selection_sort(arr):
  2. n = len(arr)
  3.  
  4. # Traverse through all array elements
  5. for i in range(n - 1):
  6. # Find the minimum element in the unsorted portion
  7. min_index = i
  8. for j in range(i + 1, n):
  9. if arr[j] < arr[min_index]:
  10. min_index = j
  11.  
  12. # Swap the found minimum element with the first element
  13. arr[i], arr[min_index] = arr[min_index], arr[i]
  14.  
  15. return arr
  16.  
  17. # Example usage
  18. arr = [64, 25, 12, 22, 11, 54, 76, 89, 19]
  19. sorted_arr = selection_sort(arr)
  20. print("Sorted array:", sorted_arr)
  21.  
  22. OUTPUT :

  23.  
  24. Sorted array: [11, 12, 19, 22, 25, 54, 64, 76, 89]
  25.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.