<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: siddhartha280601</title>
    <description>The latest articles on DEV Community by siddhartha280601 (@siddhartha280601).</description>
    <link>https://dev.to/siddhartha280601</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F611110%2Fbcd2eff3-b2f6-433f-bcab-a73e40772c1a.png</url>
      <title>DEV Community: siddhartha280601</title>
      <link>https://dev.to/siddhartha280601</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siddhartha280601"/>
    <language>en</language>
    <item>
      <title>Sorting Algorithms in Python</title>
      <dc:creator>siddhartha280601</dc:creator>
      <pubDate>Fri, 23 Apr 2021 05:52:16 +0000</pubDate>
      <link>https://dev.to/siddhartha280601/sorting-algorithms-in-python-33mm</link>
      <guid>https://dev.to/siddhartha280601/sorting-algorithms-in-python-33mm</guid>
      <description>&lt;p&gt;What do you mean by Sorting?&lt;br&gt;
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. Most common orders are in numerical or lexicographical order.&lt;br&gt;
What is the importance of Sorting?&lt;br&gt;
The importance of sorting lies in the fact that data searching can be optimized to a very high level, if data is stored in a sorted manner. Sorting is also used to represent data in more readable formats. Below we see five such implementations of sorting in python.&lt;br&gt;
1.Bubble Sort&lt;br&gt;
2.Merge Sort&lt;br&gt;
3.Insertion Sort&lt;br&gt;
4.Selection Sort&lt;br&gt;
5.Quick Sort&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bubble Sort :
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. In bubble sort, we compare each element with its adjacent neighbor or and swap if it bigger.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Time Complexity:&lt;/p&gt;

&lt;p&gt;The time complexity of best-case of bubble sort is O(n). Bubble sort has a worst-case and average complexity of O(n2), where n is the number of items being sorted.&lt;/p&gt;

&lt;p&gt;PROGRAM:&lt;/p&gt;

&lt;p&gt;def bubbleSort(nlist):&lt;br&gt;
    for passnum in range(len(nlist)-1,0,-1):&lt;br&gt;
        for i in range(passnum):&lt;br&gt;
            if nlist[i]&amp;gt;nlist[i+1]:&lt;br&gt;
                temp = nlist[i]&lt;br&gt;
                nlist[i] = nlist[i+1]&lt;br&gt;
                nlist[i+1] = temp&lt;/p&gt;

&lt;p&gt;nlist = [14,46,43,27,57,41,45,21,70]&lt;br&gt;
bubbleSort(nlist)&lt;br&gt;
print(nlist)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Merge Sort :
Merge sort is based upon divide and conquer technique. In merge sort, we divide recursively divide each sub-lists into element level, and then start merging them.Merge sort is a divide and conquer algorithm.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Time Complexity :&lt;/p&gt;

&lt;p&gt;Time complexity of Merge Sort is O(nlogn) in all the three cases (worst, average and best) as merge sort always divides the array in two halves and takes linear time to merge two halves. It requires equal amount of additional space as the unsorted array. def&lt;/p&gt;

&lt;p&gt;PROGRAM:&lt;/p&gt;

&lt;p&gt;def mergesort(unsorted_list):&lt;br&gt;
    if len(unsorted_list) &amp;lt;= 1:&lt;br&gt;
        return unsorted_list&lt;/p&gt;

&lt;h1&gt;
  
  
  Find the middle point and divide it
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;middle = len(unsorted_list) // 2
left_list = unsorted_list[:middle]
right_list = unsorted_list[middle:]

left_list = mergesort(left_list)
right_list = mergesort(right_list)
return list(merge(left_list, right_list))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Merge the sorted halves
&lt;/h1&gt;

&lt;p&gt;def merge(left_half,right_half):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res = []
while len(left_half) != 0 and len(right_half) != 0:
    if left_half[0] &amp;lt; right_half[0]:
        res.append(left_half[0])
        left_half.remove(left_half[0])
    else:
        res.append(right_half[0])
        right_half.remove(right_half[0])
if len(left_half) == 0:
    res = res + right_half
else:
    res = res + left_half
return res
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;unsorted_list = [64, 34, 25, 12, 22, 11, 90]&lt;/p&gt;

&lt;p&gt;print(mergesort(unsorted_list))&lt;/p&gt;

&lt;p&gt;3.Insertion Sort:&lt;/p&gt;

&lt;p&gt;Insertion sort is in place comparison algorithm. Here we are creating a sub-list of sorted elements from our given list, and iteratively keep on adding new elements and sort them.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;/p&gt;

&lt;p&gt;The best case input is an array that is already sorted. In this case insertion sort has a linear running time i.e., O(n).The simplest worst case input is an array sorted in reverse order,this gives insertion sort a quadratic running time i.e., O(n2).&lt;/p&gt;

&lt;p&gt;PROGRAM:&lt;/p&gt;

&lt;p&gt;def insertionsort(inputList):&lt;br&gt;
    for i in range(1, len(inputList)):&lt;br&gt;
        j = i-1&lt;br&gt;
        nxt_element = inputList[i]&lt;/p&gt;

&lt;h1&gt;
  
  
  Compare the current element with next one
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while (inputList[j] &amp;gt; nxt_element) and (j &amp;gt;= 0):
         inputList[j+1] = inputList[j]
         j=j-1
    inputList[j+1] = nxt_element
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;list = [19,2,31,45,30,11,121,27]&lt;br&gt;
insertionsort(list)&lt;br&gt;
print(list)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Selection Sort:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Selection sort is simplest of sorting algorithm. Selection sort is an in place comparison where we will keep iterating and building sorted new list from our given data. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. Selection sort is not an efficient sorting method and is not used for larger data set.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;/p&gt;

&lt;p&gt;In the worst case,outer loop executes N-1 time and inner loop executes N-i-1 comparisons. (n-1)+(n-2)+…+2+1=n*(n-1)/2 = O(n2) The worst case occurs if the array is already sorted in descending order.The Best Case is O(n2).The Average Case is O(n2).&lt;/p&gt;

&lt;p&gt;PROGRAM:&lt;/p&gt;

&lt;p&gt;def selectionsort(input_list):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for idx in range(len(input_list)):

    min_idx = idx
    for j in range( idx +1, len(input_list)):
        if input_list[min_idx] &amp;gt; input_list[j]:
            min_idx = j
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Swap the minimum value with the compared value
&lt;/h1&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    input_list[idx], input_list[min_idx] = input_list[min_idx], input_list[idx]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;l = [19,2,31,45,30,11,121,27]&lt;br&gt;
selectionsort(l)&lt;br&gt;
print(l)&lt;/p&gt;

&lt;p&gt;5.Quick Sort:&lt;/p&gt;

&lt;p&gt;Quick sort is highly efficient sorting algorithm which is often used for large data sets. Quick sort is based upon partitioning list into smaller lists (based on pivot point). Elements are arranged on basis of whether they are smaller or larger than pivot.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;/p&gt;

&lt;p&gt;Worst Case Time Complexity of Quick sort is O(n2).Best Case and Average Time Complexity is O(nlogn).&lt;/p&gt;

&lt;p&gt;PROGRAM:&lt;/p&gt;

&lt;h1&gt;
  
  
  divide function
&lt;/h1&gt;

&lt;p&gt;def partition(arr,low,high):&lt;br&gt;
   i = ( low-1 )&lt;br&gt;
   pivot = arr[high] # pivot element&lt;br&gt;
   for j in range(low , high):&lt;br&gt;
       # If current element is smaller&lt;br&gt;
       if arr[j] &amp;lt;= pivot:&lt;br&gt;
          # increment&lt;br&gt;
          i = i+1&lt;br&gt;
          arr[i],arr[j] = arr[j],arr[i]&lt;br&gt;
   arr[i+1],arr[high] = arr[high],arr[i+1]&lt;br&gt;
   return ( i+1 )&lt;/p&gt;

&lt;h1&gt;
  
  
  sort
&lt;/h1&gt;

&lt;p&gt;def quickSort(arr,low,high):&lt;br&gt;
   if low &amp;lt; high:&lt;br&gt;
      # index&lt;br&gt;
      pi = partition(arr,low,high)&lt;br&gt;
      # sort the partitions&lt;br&gt;
      quickSort(arr, low, pi-1) &lt;br&gt;
      quickSort(arr, pi+1, high)&lt;/p&gt;

&lt;h1&gt;
  
  
  main
&lt;/h1&gt;

&lt;p&gt;arr = [2,5,3,8,6,5,4,7]&lt;br&gt;
n = len(arr)&lt;br&gt;
quickSort(arr,0,n-1)&lt;br&gt;
for i in range(n):&lt;br&gt;
    print (arr[i],end=" ")&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
