From 120434c82bb9b8b09a500170e0be9566bae8ad08 Mon Sep 17 00:00:00 2001 From: Aishwary Gupta <115387744+Aishwary2004Gupta@users.noreply.github.com> Date: Fri, 27 Oct 2023 23:08:19 +0530 Subject: [PATCH 1/2] QuickSort.java Quick Sort algorithm using recursion --- QuickSort.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 QuickSort.java diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 000000000000..be384e28f916 --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,39 @@ +public class quickSort { + public static void main(String[] args) { + int[] arr = {5,6,4,2,6,1,0}; + sort(arr, 0, arr.length-1); + System.out.println(Arrays.toString(arr)); + + } + + + static void sort(int[] nums, int low, int high) { + if (low >= high){ + return; + } + + int start = low; + int end = high; + int mid = start + (end - start)/2; + int pivot = nums[mid]; //contains the value + + while (start <= end){ + while (nums[start] < pivot){ + start++; + } + while (nums[end] >pivot){ + end--; + } + + if (start <= end){ + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } + } + sort(nums, low, end); + sort(nums, start, high); + } +} From c4d382068b676ffb113daf6bb5d5645bfa7c9d8a Mon Sep 17 00:00:00 2001 From: Aishwary Gupta <115387744+Aishwary2004Gupta@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:03:53 +0530 Subject: [PATCH 2/2] Create SelectionSort.java Using recursion --- SelectionSort.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 SelectionSort.java diff --git a/SelectionSort.java b/SelectionSort.java new file mode 100644 index 000000000000..4ef4350eb222 --- /dev/null +++ b/SelectionSort.java @@ -0,0 +1,26 @@ +public class SelectionSort { + public static void main(String[] args) { + int[] arr = {5,6,2,1,0,-2,-5}; + selectionSort(arr, arr.length, 0,0); + System.out.println(Arrays.toString(arr)); + } + static void selectionSort(int[] arr, int row, int col, int max){ //max = index + if (row == 0){ + return; + } + + if (col < row){ + if (arr[col] > arr[max]) { //element + selectionSort(arr, row, col+1, col); //max will be equal to col + }else { + selectionSort(arr, row, col + 1, max); + } //now there is a maximum + }else { //col == row, go to next row + int temp = arr[max]; + arr[max] = arr[row-1]; //last element in that row + arr[row-1] = temp; + + selectionSort(arr,row - 1, 0,0); //max = 0th index + } + } +}