<jupyter_start><jupyter_text>"Selection Sort"> "Ins and Outs of selection sort algorithm. with maximum and minimum approaches"- toc: true - badges: true- comments: true- categories: [Algorithms]- comments: true- author: <NAME>  IntroductionThe selection sort can be implemented in two approaches by considering the largest elements or the smallest elements. Here we are considering the maximum elements sorting procedure.- The selection sort improves on the bubble sort by making only one exchange for every pass through the list.- A selection sort looks for the largest value as it makes a pass and, after completing the pass, places in the proper location.- After the first pass, the largest item is in the correct place. After the second pass, the next largest is in place.- This process continues and requires $(n-1)$ passes to sort $n$ items, since the final item must be in place after the $(n-1)st$ pass ![](Images/Algorithms/selectionsort.PNG)  Implementation of Selection Sort considering maximum elements<jupyter_code>#collapse-hide
def selection_sort(arr):
    
    # For every slot in array
    for fillslot in range(len(arr)-1,0,-1):
        positionOfMax=0
        
        # For every set of 0 to fillslot+1
        for location in range(1,fillslot+1):
            # Set maximum's location
            if arr[location]>arr[positionOfMax]:
                positionOfMax = location

        temp = arr[fillslot]
        arr[fillslot] = arr[positionOfMax]
        arr[positionOfMax] = temp
        
        return arr
selection_sort(arr)<jupyter_output><empty_output><jupyter_text>Algorithm considering smallest elements- The algorithm proceedss by finding the smallest element in the unsorted subarrya- Exchange/ Swap it with the leftmost unsorted element -> putting it in sorted order- Moving the subarray boundaries one element to the right  Pseduocode ![](Images/Algorithms/selectionsort_pseducode.PNG)  Implementation of Selection Sort considering minimum elements<jupyter_code>#collapse-hide
def selectionSort(arr):
    for i in range(0, len(arr)-1):
        index = i
        
        for j in range(i+1, len(arr), 1):
            # For descending order use `>`
                if arr[j] < arr[index]:
                    index = j
        if index != i:
            arr[index], arr[i] = arr[i], arr[index]
    
    return arr
selectionSort(arr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07602339181286549, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 3.3075539568345325, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '451331', 'license': 'Apache-2.0', 'n_tokens_mistral': 639, 'n_tokens_neox': 615, 'n_words': 267, 'path': '_notebooks/2020-05-06-Selection-Sort.ipynb', 'repo_name': 'tejakummarikuntla/notes', 'size': '2406'}