From f1dd31861cd309e67160a9f2db3a8fd7d519230a Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:01:32 +0530 Subject: [PATCH 1/6] bubblesort.py Python prog. for Bubble sort. --- Sorting Algorithims/bubblesort.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Sorting Algorithims/bubblesort.py diff --git a/Sorting Algorithims/bubblesort.py b/Sorting Algorithims/bubblesort.py new file mode 100644 index 00000000000..473dc33dce0 --- /dev/null +++ b/Sorting Algorithims/bubblesort.py @@ -0,0 +1,26 @@ +# Python program for implementation of Bubble Sort + +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n-1): + # range(n) also work but outer loop will repeat one time more than needed. + + # Last i elements are already in place + for j in range(0, n-i-1): + + # traverse the array from 0 to n-i-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] + +# Driver code to test above +arr = [64, 34, 25, 12, 22, 11, 90] + +bubbleSort(arr) + +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]), From b89a7c99d80f5f7fe8bb22f8f30c397560ad4bec Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:07:38 +0530 Subject: [PATCH 2/6] QuickSort.py python prog. for quick sort. --- Sorting Algorithims/QuickSort.py | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Sorting Algorithims/QuickSort.py diff --git a/Sorting Algorithims/QuickSort.py b/Sorting Algorithims/QuickSort.py new file mode 100644 index 00000000000..89b97c181c5 --- /dev/null +++ b/Sorting Algorithims/QuickSort.py @@ -0,0 +1,56 @@ +# Python program for implementation of Quicksort Sort + +# This function takes last element as pivot, places +# the pivot element at its correct position in sorted +# array, and places all smaller (smaller than pivot) +# to left of pivot and all greater elements to right +# of pivot + + +def partition(arr, low, high): + i = (low-1) # index of smaller element + pivot = arr[high] # pivot + + for j in range(low, high): + + # If current element is smaller than or + # equal to pivot + if arr[j] <= pivot: + + # increment index of smaller element + i = i+1 + arr[i], arr[j] = arr[j], arr[i] + + arr[i+1], arr[high] = arr[high], arr[i+1] + return (i+1) + +# The main function that implements QuickSort +# arr[] --> Array to be sorted, +# low --> Starting index, +# high --> Ending index + +# Function to do Quick sort + + +def quickSort(arr, low, high): + if len(arr) == 1: + return arr + if low < high: + + # pi is partitioning index, arr[p] is now + # at right place + pi = partition(arr, low, high) + + # Separately sort elements before + # partition and after partition + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) + + +# Driver code to test above +arr = [10, 7, 8, 9, 1, 5] +n = len(arr) +quickSort(arr, 0, n-1) +print("Sorted array is:") +for i in range(n): + print("%d" % arr[i]) From ce172f5ca0d5130d3cb48ddd61bf837690772330 Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:09:38 +0530 Subject: [PATCH 3/6] InsertionSort.py python prog. for insertion sort. --- Sorting Algorithims/InsertionSort.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Sorting Algorithims/InsertionSort.py diff --git a/Sorting Algorithims/InsertionSort.py b/Sorting Algorithims/InsertionSort.py new file mode 100644 index 00000000000..b55d067510a --- /dev/null +++ b/Sorting Algorithims/InsertionSort.py @@ -0,0 +1,26 @@ +# Python program for implementation of Insertion Sort + +# Function to do insertion sort +def insertionSort(arr): + + # Traverse through 1 to len(arr) + for i in range(1, len(arr)): + + key = arr[i] + + # Move elements of arr[0..i-1], that are + # greater than key, to one position ahead + # of their current position + j = i-1 + while j >=0 and key < arr[j] : + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key + + +# Driver code to test above +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]) From a58da6ace05ebc4b0e459e6a364c51db6dc3a432 Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:12:51 +0530 Subject: [PATCH 4/6] HeapSort.py python prog. for heap sort. --- Sorting Algorithims/HeapSort.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Sorting Algorithims/HeapSort.py diff --git a/Sorting Algorithims/HeapSort.py b/Sorting Algorithims/HeapSort.py new file mode 100644 index 00000000000..9a2e2c1444a --- /dev/null +++ b/Sorting Algorithims/HeapSort.py @@ -0,0 +1,33 @@ +# heapify +def heapify(arr, n, i): + largest = i # largest value + l = 2 * i + 1 # left + r = 2 * i + 2 # right + # if left child exists + if l < n and arr[i] < arr[l]: + largest = l + # if right child exits + if r < n and arr[largest] < arr[r]: + largest = r + # root + if largest != i: + arr[i],arr[largest] = arr[largest],arr[i] # swap + # root. + heapify(arr, n, largest) +# sort +def heapSort(arr): + n = len(arr) + # maxheap + for i in range(n, -1, -1): + heapify(arr, n, i) + # element extraction + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # swap + heapify(arr, i, 0) +# main +arr = [2,5,3,8,6,5,4,7] +heapSort(arr) +n = len(arr) +print ("Sorted array is") +for i in range(n): + print (arr[i],end=" ") From 919d98ef4e46663082243b947336807024123929 Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:15:59 +0530 Subject: [PATCH 5/6] CycleSort.py python prog. for cycle sort. --- Sorting AlgorithimsSort.py/CycleSort.py | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Sorting AlgorithimsSort.py/CycleSort.py diff --git a/Sorting AlgorithimsSort.py/CycleSort.py b/Sorting AlgorithimsSort.py/CycleSort.py new file mode 100644 index 00000000000..0b6dc03b6a5 --- /dev/null +++ b/Sorting AlgorithimsSort.py/CycleSort.py @@ -0,0 +1,38 @@ +def cycleSort(array): + writes = 0 + # cycles to be rotated + for cycleStart in range(0, len(array) - 1): + item = array[cycleStart] + #position to place the item + pos = cycleStart + for i in range(cycleStart + 1, len(array)): + if array[i] < item: + pos += 1 + # if item exits, it is not a cycle + if pos == cycleStart: + continue + # Otherwise, place the item + while item == array[pos]: + pos += 1 + array[pos], item = item, array[pos] + writes += 1 + # rotation continued + while pos != cycleStart: + # Find a position to place the item + pos = cycleStart + for i in range(cycleStart + 1, len(array)): + if array[i] < item: + pos += 1 + # place the item + while item == array[pos]: + pos += 1 + array[pos], item = item, array[pos] + writes += 1 + return writes +# main +arr = [1,5,3,4,8,6,3,4,5] +n = len(arr) +cycleSort(arr) +print("Sorted array is : ") +for i in range(0, n) : + print(arr[i], end = " ") From 2179489dfab6a95c85fb71e4ce00f977aa9874f0 Mon Sep 17 00:00:00 2001 From: JAYANTI MALA <52104784+jayantimala@users.noreply.github.com> Date: Fri, 2 Oct 2020 12:17:48 +0530 Subject: [PATCH 6/6] CountingSort.py python prog. for counting sort. --- Sorting AlgorithimsSort.py/CountingSort.py | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Sorting AlgorithimsSort.py/CountingSort.py diff --git a/Sorting AlgorithimsSort.py/CountingSort.py b/Sorting AlgorithimsSort.py/CountingSort.py new file mode 100644 index 00000000000..d61b3d7807b --- /dev/null +++ b/Sorting AlgorithimsSort.py/CountingSort.py @@ -0,0 +1,25 @@ +def countSort(arr): + # The output character array that will have sorted arr + output = [0 for i in range(256)] + # Create a count array initialized with 0 + count = [0 for i in range(256)] + # as strings are immutable + ans = ["" for _ in arr] + # count + for i in arr: + count[ord(i)] += 1 + # position of character in the output array + for i in range(256): + count[i] += count[i-1] + # output character array + for i in range(len(arr)): + output[count[ord(arr[i])]-1] = arr[i] + count[ord(arr[i])] -= 1 + # array of sorted charcters + for i in range(len(arr)): + ans[i] = output[i] + return ans +# main +arr = "Tutorialspoint" +ans = countSort(arr) +print ("Sorted character array is "+str("".join(ans))