Skip to main content

Selection Sort

Selection sort is a sorting algorithm in which the list is considered into two parts, sorted part at left and unsorted part at right. It selects the smallest element from the unsorted list, in each iteration, and places that element at the beginning of unsorted list. Intitially, the sorted part in empty, that is, the whole list is unsorted. With each iteration, the smallest element is swapped with the leftmost element of the unsorted part of the list. After required iterations, we obtain a sorted list of elements.

Illustration:

Algorithm:

for all elements in list,step=from step=0 to step=length(list)-1
    min_idx=step
    for i from step+1 to length(list)-1 
        if value at index i < value at index min_idx
            min_idx = i
    swap as (list[step],list[min_idx])=(list[min_idx],list[step])

Time Complexity:
  • Worst Case: O(n2)
  • Average Case: O(n2)
  • Best Case: O(n2)
Space Complexity: O(1)

Program for Selection Sort:


Output:

Popular posts from this blog

Image to Pencil Sketch using Python and OpenCV

In this post, we will go through a program to get a pencil sketch from an image using python and OpenCV.  Step 1:  To use OpenCV, import the library. Step 2: Read the Image. Step 3: Create a new image by converting the original image to grayscale. Step 4: Invert the grayscale image. We can invert images simply by subtracting from 255, as grayscale images are 8 bit images or have a maximum of 256 tones. Step 5: Blur the inverted image using GaussianBlur  method in OpenCV library and invert the blurred image.  Step 6: Divide the grayscale values of the image by the values of image received from step-5 ( Note: We inverted the grayscale image and we blurred this image and then again inverted it ). Diving an image from its smoothened form will highlight the edges and we get the image like Pencil Sketch. Steps Illustration: Code: Execution Output: