From a037647a1cc742cd3f9cccc3d6c6b634e52c8c38 Mon Sep 17 00:00:00 2001 From: Sandra Santhosh <102308614+aquiladive@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:46:23 +0530 Subject: [PATCH 1/3] Create JavaImplementation.java Java Program for Quick Sort. --- .../Quick Sort/JavaImplementation.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Sorting Algorithms/Quick Sort/JavaImplementation.java diff --git a/Sorting Algorithms/Quick Sort/JavaImplementation.java b/Sorting Algorithms/Quick Sort/JavaImplementation.java new file mode 100644 index 0000000..f6858a2 --- /dev/null +++ b/Sorting Algorithms/Quick Sort/JavaImplementation.java @@ -0,0 +1,38 @@ +public class QuickSort { + static int n=10; + static int a[]={5, 3, 12, 78, 32, 51, 16, 75, 8, 33}; + public static void main(String[] args) { + QuickAlg(0,n-1); + DisplayArray(); + } + public static void QuickAlg(int p, int r) { + int i,j,temp; + i=p+1; + j=r; + if(pa[p]) + j--; + if(i Date: Mon, 9 Oct 2023 23:31:01 +0530 Subject: [PATCH 2/3] Create Theory.md --- Sorting Algorithms/Quick Sort/Theory.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sorting Algorithms/Quick Sort/Theory.md diff --git a/Sorting Algorithms/Quick Sort/Theory.md b/Sorting Algorithms/Quick Sort/Theory.md new file mode 100644 index 0000000..c68b92f --- /dev/null +++ b/Sorting Algorithms/Quick Sort/Theory.md @@ -0,0 +1,14 @@ +## Quicksort + +Quicksort uses the divide-and-conquer algorithmic technique to sort elements. An element ('pivot') is selected from the array and array is partitioned into two sub-arrays, according to whether they are less than or greater than it. The subarrays are recursively sorted by applying quicksort to each of them. + +Quicksort is in-place but it is not stable. + +In its best and average case, it has a time complexity of O(nlogn). Its worst case time complexity is O(n^2). + +#### Steps for Quicksort: + +- Choose an element ('pivot'). +- Rearrange array so that the correct postion of pivot in the final sorted array is found, and once pivot is moved here, elements to left of pivot are smaller than it and elements to the right of pivot are greater than it (they do not have to be sorted yet). +- This splits the array into two subarrays. +- Apply these steps to the two subarrays also recursively until the entire array is sorted. From b2a0ae559ed2674ac3b3aaebd1ffe901b133d6fb Mon Sep 17 00:00:00 2001 From: Sandra Santhosh <102308614+aquiladive@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:46:13 +0530 Subject: [PATCH 3/3] Update Theory.md Changes to the steps. --- Sorting Algorithms/Quick Sort/Theory.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Sorting Algorithms/Quick Sort/Theory.md b/Sorting Algorithms/Quick Sort/Theory.md index c68b92f..5971d4d 100644 --- a/Sorting Algorithms/Quick Sort/Theory.md +++ b/Sorting Algorithms/Quick Sort/Theory.md @@ -1,14 +1,11 @@ ## Quicksort -Quicksort uses the divide-and-conquer algorithmic technique to sort elements. An element ('pivot') is selected from the array and array is partitioned into two sub-arrays, according to whether they are less than or greater than it. The subarrays are recursively sorted by applying quicksort to each of them. - -Quicksort is in-place but it is not stable. - -In its best and average case, it has a time complexity of O(nlogn). Its worst case time complexity is O(n^2). +Quicksort uses the divide-and-conquer algorithmic technique to sort elements. It is an in-place sorting algorithm but it is not stable. In its best and average case, it has a time complexity of O(nlogn). Its worst case time complexity is O(n^2). #### Steps for Quicksort: - Choose an element ('pivot'). -- Rearrange array so that the correct postion of pivot in the final sorted array is found, and once pivot is moved here, elements to left of pivot are smaller than it and elements to the right of pivot are greater than it (they do not have to be sorted yet). +- Rearrange the array until a point of division is found such that when pivot is placed there, elements to the left are smaller than pivot and elements to the right are greater than pivot. This will also be the correct postion of pivot in the final sorted array. +- Move pivot to that position. - This splits the array into two subarrays. -- Apply these steps to the two subarrays also recursively until the entire array is sorted. +- Apply these steps to the two subarrays recursively until the entire array is sorted.