

![def mergeSort (alist): if len (alist)>1: mid - len (alist)//2 lefthalf-alist[:mid] righthalf-alist [mid:] mergesort (lefthalf](//img.homeworklib.com/images/5a82aa96-f28b-46a2-bb98-17d74586835f.png?x-oss-process=image/resize,w_560)
![files size 100 . unsorted, size 1000·unsorted, size #implementing sorting algorithms inc- 100 10000·unsorted ] for i i](//img.homeworklib.com/images/d947bb18-cf2e-4bb8-9368-0d3550914758.png?x-oss-process=image/resize,w_560)

time complexities:
Bubble Sort -
best - n
worst - n^2
selection Sort -
best - n^2
worst - n^2
insertion sort -
best - n
worst - n^2
Merge Sort -
best - n log(n)
worst - n log(n)
Qucik sort -
best - n log(n)
worst - n^2
The bubble sort is one of the slowest sorting algorithms, but it is also one of the easiest sorts to implement.
selection sort works by starting at the beginning of the array and comparing the first element with the remaining elements.
After examining all the elements, the smallest element is placed in the first position of the array, and the algorithm moves to
the second position.
This process continues until the data is sorted.
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient
on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides\
several advantages.
The Quicksort algorithm is one of the fastest sorting algorithms for large data sets. Quicksort is a divide-and-conquer
algorithm that recursively breaks a list of data into successively smaller sublists consisting of the smaller elements and the
larger elements. The algorithm continues this process until all the data in the list is sorted.
The algorithm divides the list into
sublists by selecting one element of the list as a pivot. Data is sorted around the pivot by moving elements less than the pivot
to the bottom of the list and elements that are greater than the pivot to the top of the list.
MergeSort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then
merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that
assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
import random import sys import time sys.setrecursionlimit (10000) def bubbleSort (arr): n - len (arr) for i in range (n): # Last 1 elements are already in place for j in range (0, n-i-1): # traverse the array from 0 to n-1-1 # swap if the element found is greater # than the next element If arr [j] > arr[j+1] arr [j], arr [j+1] : arr [j+1], arr [j] return arr def selectionSort (arr): # Traverse through all array elements for i in range (len (arr)): # Find the minimum element in remaining # unsorted array min idxi for j in range (i+1, len (arr)) if arr[min idx] > arr[j]: min idx - j # swap the found minimum element with # the first element arr[il, arr[min idx] - arr[min idx], arr[i] return arr def insertionSort (arr): # Traverse through 1 to len (arr) for i in range (1, len (arr)): key = arr [1] # Move elements of arr[0..
1-1], that are # greater than key, to one position ahead # of their current position ji-1 while J >= 0 and key < arr [j] : arr [j + 1] = arr [j] arr [j 1] = key return arr
def quickSort (alist, start, end): 'Sorts the list from indexes start to end- 1 inclusive.'"' if endstart 1: p -partition (alist, start, end) quicksort (alist, start, p) quicksort (alist, p + 1, end) return alist def partition (alist, start, end): pivot - alist[start] i start 1 j - end -1 while True: while (i <- j and alist[i] pivot) while (i <- j and alistlj] pivot) 1 alist[i], alist [j-alist[j], alist[i] else: alist[start], alist[j]-alist[j], alist[start] return
def mergeSort (alist): if len (alist)>1: mid - len (alist)//2 lefthalf-alist[:mid] righthalf-alist [mid:] mergesort (lefthalf) mergeSort (righthalf) while i < len (lefthalf) and j < len (righthalf): if lefthalf[i] < righthalf [j]: alist[k]-lefthalf [i] i-i+1 else: alist[k]-righthalf[j] j-j+1 k-k+1 while i < len (lefthalf) alist[k]-lefthalf [i] i-i+1 k-k+1 while j < len (righthalf): alist[k]-righthalf[j] j-j+1 k-k+1 return alist
files ' size 100 . unsorted', 'size 1000·unsorted', 'size #implementing sorting algorithms inc- 100 10000·unsorted '] for i in range (3) f-open (files[i], 'w+') unsorted- random. sample (range (inc),inc) f.write(' '.join (map (str,unsorted))) f.close() copy - unsorted start-time.time () bubble -bubblesort (copy) end-time.time () print ('bubble sort '+str (inc) +":",end-start) copy - unsorted start-time.time () selection - selectionSort (copy) end-time.time () print ('selection sort '+str (inc) +": " ,end-start) copy - unsorted start-time.time () insertion - insertionSort (copy) end-time.time () print('insertion sort '+str (inc) +": " ,end-start) copy - unsorted start-time.time () quick - quicksort (copy, 0,len (copy)) end-time.time () print ('quick sort '+str (inc)+":",end-start) copy - unsorted start-time.time () merge - mergeSort (copy) end-time.time () print ('merge sort '+str (inc)+":" ,end-start)
f = open ( ' size ' +str (inc) + ' . sorted. bubble ' , 'w+ ' ) f.write(' '.join (map (str,bubble))) f.close () f-open ('size '+str(inc) +'.sorted.selection', 'w+') f.write (' ".join (map (str,selection))) f.close () f-open ('size '+str(inc) +'.sorted.insertion', 'w+') f.write(' '.join (map (str,insertion))) f.close () fopen ('size +str (inc) +'.sorted.quick', 'w+') f.write (' .join (map (str,quick))) f.close () fopen ('size +str (inc) +'.sorted.merge, 'w+') f.write (' .join (map (str,merge))) f.close () inc 10
.