From 17e8701ee8d63b1127a8094a86c6ed0758759c4a Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 3 Jun 2016 08:53:18 +0530 Subject: [PATCH 1/6] Create Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Algorithms-Selection-Sort.md diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md new file mode 100644 index 0000000000..8517992145 --- /dev/null +++ b/Algorithms-Selection-Sort.md @@ -0,0 +1,49 @@ +# Algorithm Selection Sort + +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and +putting it at the beginning. The algorithm maintains two subarrays in a given array. +1) The subarray which is already sorted. +2) Remaining subarray which is unsorted. +In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to +the sorted subarray. + +#### Example +[Animation of SelectionSort](http://www.sorting-algorithms.com/selection-sort) + +``` +arr[] = 64 25 12 22 11 + +#Placing the minimum element in arr[0...4] in the beginning +11 25 12 22 64 + +#Placing the minimum element in arr[1...4] in the beginning +11 12 25 22 64 + +#Placing the minimum element in arr[2...4] in the beginning +11 12 22 25 64 + +#Placing the minimum element in arr[3...4] in the beginning +11 12 22 25 64 +``` + +#### Python Implementation + +```python +def selection_sort(arr): + for i in range(len(arr)): + min_x = i + for j in range(i+1,len(arr)): + if arr[j] < arr[min_x]: + min_x = j + arr[min_x], arr[i] = arr[i], arr[min_x] + +arr = [64, 25, 12, 22, 11] +selection_sort(arr) +print(arr) # Prints [11, 12, 22, 25, 64] +``` + +:rocket: [Run Code](https://repl.it/CXwQ) + +#### [Complexity of Algorithm](https://www.freecodecamp.com/videos/big-o-notation-what-it-is-and-why-you-should-care) + +**Time Complexity:** O(n*n) Due to the two nested loops. From 5c0f046ef8e02a8c6761149f6f979294130db193 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 3 Jun 2016 08:54:29 +0530 Subject: [PATCH 2/6] Update Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md index 8517992145..abaf05b7bb 100644 --- a/Algorithms-Selection-Sort.md +++ b/Algorithms-Selection-Sort.md @@ -1,11 +1,9 @@ # Algorithm Selection Sort -The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and -putting it at the beginning. The algorithm maintains two subarrays in a given array. +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. -2) Remaining subarray which is unsorted. -In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to -the sorted subarray. +2) Remaining subarray which is unsorted. +In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. #### Example [Animation of SelectionSort](http://www.sorting-algorithms.com/selection-sort) From 81ea2100ab291b1f485158b359b5e6bc34388247 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 3 Jun 2016 08:55:20 +0530 Subject: [PATCH 3/6] Update Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md index abaf05b7bb..75ac189f7f 100644 --- a/Algorithms-Selection-Sort.md +++ b/Algorithms-Selection-Sort.md @@ -1,8 +1,8 @@ # Algorithm Selection Sort -The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. -1) The subarray which is already sorted. -2) Remaining subarray which is unsorted. +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. +1) The subarray which is already sorted. +2) Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. #### Example From afe359e05e50fc7008201b0865e9c2b2e0103bea Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 3 Jun 2016 08:55:43 +0530 Subject: [PATCH 4/6] Update Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md index 75ac189f7f..6662490cac 100644 --- a/Algorithms-Selection-Sort.md +++ b/Algorithms-Selection-Sort.md @@ -3,6 +3,7 @@ The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. + In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. #### Example From 59a85db8cad3a5fdbdbb1d68a3b519e23b56b729 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 3 Jun 2016 19:41:15 +0530 Subject: [PATCH 5/6] Update Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md index 6662490cac..803a4a6d5c 100644 --- a/Algorithms-Selection-Sort.md +++ b/Algorithms-Selection-Sort.md @@ -1,6 +1,6 @@ # Algorithm Selection Sort -The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. @@ -12,16 +12,16 @@ In every iteration of selection sort, the minimum element (considering ascending ``` arr[] = 64 25 12 22 11 -#Placing the minimum element in arr[0...4] in the beginning +# Placing the minimum element in arr[0...4] in the beginning 11 25 12 22 64 -#Placing the minimum element in arr[1...4] in the beginning +# Placing the minimum element in arr[1...4] in the beginning 11 12 25 22 64 -#Placing the minimum element in arr[2...4] in the beginning +# Placing the minimum element in arr[2...4] in the beginning 11 12 22 25 64 -#Placing the minimum element in arr[3...4] in the beginning +# Placing the minimum element in arr[3...4] in the beginning 11 12 22 25 64 ``` From c309f7ac9c59a61f598a206d74dbcf9996d52c8a Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Mon, 6 Jun 2016 06:07:00 +0530 Subject: [PATCH 6/6] Update Algorithms-Selection-Sort.md --- Algorithms-Selection-Sort.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Algorithms-Selection-Sort.md b/Algorithms-Selection-Sort.md index 803a4a6d5c..5e3874e719 100644 --- a/Algorithms-Selection-Sort.md +++ b/Algorithms-Selection-Sort.md @@ -1,12 +1,12 @@ # Algorithm Selection Sort The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. -1) The subarray which is already sorted. -2) Remaining subarray which is unsorted. +1. The subarray which is already sorted. +2. Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. -#### Example +## Example [Animation of SelectionSort](http://www.sorting-algorithms.com/selection-sort) ```