From 7e1383cf4dc59d21e6a2d6f16871c7ac028d8e00 Mon Sep 17 00:00:00 2001 From: Autithi Paul <78257809+Autithi2002@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:11:16 +0530 Subject: [PATCH] Added to various sorting code. please add my file to your repository with the label Hactoberfest. --- Bubble sort.txt | 33 +++++++++++++++++++ heap sort.txt | 72 +++++++++++++++++++++++++++++++++++++++++ mergesort.txt | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ quicksort.txt | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 Bubble sort.txt create mode 100644 heap sort.txt create mode 100644 mergesort.txt create mode 100644 quicksort.txt diff --git a/Bubble sort.txt b/Bubble sort.txt new file mode 100644 index 0000000..6f25597 --- /dev/null +++ b/Bubble sort.txt @@ -0,0 +1,33 @@ +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 < n; ++i) + System.out.print(arr[i] + " "); + System.out.println(); + } + + // Driver method to test above + public static void main(String args[]) + { + BubbleSort ob = new BubbleSort(); + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; + ob.bubbleSort(arr); + System.out.println("Sorted array"); + ob.printArray(arr); + } +} \ No newline at end of file diff --git a/heap sort.txt b/heap sort.txt new file mode 100644 index 0000000..6c9e999 --- /dev/null +++ b/heap sort.txt @@ -0,0 +1,72 @@ +public class HeapSort { + public void sort(int arr[]) + { + int N = arr.length; + + // Build heap (rearrange array) + for (int i = N / 2 - 1; i >= 0; i--) + heapify(arr, N, i); + + // One by one extract an element from heap + for (int i = N - 1; i > 0; i--) { + // Move current root to end + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + + // call max heapify on the reduced heap + heapify(arr, i, 0); + } + } + + // To heapify a subtree rooted with node i which is + // an index in arr[]. n is size of heap + void heapify(int arr[], int N, int i) + { + int largest = i; // Initialize largest as root + int l = 2 * i + 1; // left = 2*i + 1 + int r = 2 * i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < N && arr[l] > arr[largest]) + largest = l; + + // If right child is larger than largest so far + if (r < N && arr[r] > arr[largest]) + largest = r; + + // If largest is not root + if (largest != i) { + int swap = arr[i]; + arr[i] = arr[largest]; + arr[largest] = swap; + + // Recursively heapify the affected sub-tree + heapify(arr, N, largest); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int N = arr.length; + + for (int i = 0; i < N; ++i) + System.out.print(arr[i] + " "); + System.out.println(); + } + + // Driver's code + public static void main(String args[]) + { + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int N = arr.length; + + // Function call + HeapSort ob = new HeapSort(); + ob.sort(arr); + + System.out.println("Sorted array is"); + printArray(arr); + } +} \ No newline at end of file diff --git a/mergesort.txt b/mergesort.txt new file mode 100644 index 0000000..88776ec --- /dev/null +++ b/mergesort.txt @@ -0,0 +1,85 @@ +class Merge { + +/* Function to merge the subarrays of a[] */ +void merge(int a[], int beg, int mid, int end) +{ + int i, j, k; + int n1 = mid - beg + 1; + int n2 = end - mid; + + /* temporary Arrays */ + int LeftArray[] = new int[n1]; + int RightArray[] = new int[n2]; + + /* copy data to temp arrays */ + for (i = 0; i < n1; i++) + LeftArray[i] = a[beg + i]; + for (j = 0; j < n2; j++) + RightArray[j] = a[mid + 1 + j]; + + i = 0; /* initial index of first sub-array */ + j = 0; /* initial index of second sub-array */ + k = beg; /* initial index of merged sub-array */ + + while (i < n1 && j < n2) + { + if(LeftArray[i] <= RightArray[j]) + { + a[k] = LeftArray[i]; + i++; + } + else + { + a[k] = RightArray[j]; + j++; + } + k++; + } + while (i Array to be sorted, + low --> Starting index, + high --> Ending index + */ + static void quickSort(int[] arr, int low, int high) + { + if (low < high) { + + // pi is partitioning index, arr[p] + // is now at right place + int pi = partition(arr, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } + } + + // Function to print an array + static void printArray(int[] arr, int size) + { + for (int i = 0; i < size; i++) + System.out.print(arr[i] + " "); + + System.out.println(); + } + + // Driver Code + public static void main(String[] args) + { + int[] arr = { 10, 7, 8, 9, 1, 5 }; + int n = arr.length; + + quickSort(arr, 0, n - 1); + System.out.println("Sorted array: "); + printArray(arr, n); + } +} \ No newline at end of file