From 782313a82e56a9748cdccc266a63ec7fa3a57c22 Mon Sep 17 00:00:00 2001 From: Fazil Khan <76592655+comedianfazil@users.noreply.github.com> Date: Wed, 20 Oct 2021 12:26:37 +0530 Subject: [PATCH 1/2] Create Heap_sort.py --- Programming_Languages/Python/Heap_sort.py | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Programming_Languages/Python/Heap_sort.py diff --git a/Programming_Languages/Python/Heap_sort.py b/Programming_Languages/Python/Heap_sort.py new file mode 100644 index 0000000..6261af6 --- /dev/null +++ b/Programming_Languages/Python/Heap_sort.py @@ -0,0 +1,48 @@ +# Python program for implementation of heap Sort + +# To heapify subtree rooted at index i. +# n is size of heap +def heapify(arr, n, i): + largest = i # Initialize largest as root + l = 2 * i + 1 # left = 2*i + 1 + r = 2 * i + 2 # right = 2*i + 2 + + # See if left child of root exists and is + # greater than root + if l < n and arr[i] < arr[l]: + largest = l + + # See if right child of root exists and is + # greater than root + if r < n and arr[largest] < arr[r]: + largest = r + + # Change root, if needed + if largest != i: + arr[i],arr[largest] = arr[largest],arr[i] # swap + + # Heapify the root. + heapify(arr, n, largest) + +# The main function to sort an array of given size +def heapSort(arr): + n = len(arr) + + # Build a maxheap. + # Since last parent will be at ((n//2)-1) we can start at that location. + for i in range(n // 2 - 1, -1, -1): + heapify(arr, n, i) + + # One by one extract elements + for i in range(n-1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] # swap + heapify(arr, i, 0) + +# Driver code to test above +arr = [ 112, 110, 183, 50, 116, 70] +heapSort(arr) +n = len(arr) +print ("Sorted array is") +for i in range(n): + print ("%d" %arr[i]), + From 0d9b1b584300b04b81753586087e183f5d13beee Mon Sep 17 00:00:00 2001 From: Fazil Khan <76592655+comedianfazil@users.noreply.github.com> Date: Wed, 20 Oct 2021 12:35:15 +0530 Subject: [PATCH 2/2] Create Bubble_sort.java --- Programming_Languages/Java/Bubble_sort.java | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Programming_Languages/Java/Bubble_sort.java diff --git a/Programming_Languages/Java/Bubble_sort.java b/Programming_Languages/Java/Bubble_sort.java new file mode 100644 index 0000000..aa85d07 --- /dev/null +++ b/Programming_Languages/Java/Bubble_sort.java @@ -0,0 +1,36 @@ +// Java program for implementation of Bubble Sort +class BubbleSort +{ + void bubbleSort(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n-1; i++) + for (int j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + { + // swap arr[j+1] and arr[j] + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + + /* Prints the array */ + void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i