Understanding Quick Sort

I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .
Quick Sort is a divide and conquer algorithm. It works by selecting an element in the array as a Pivot and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the Pivot. The sub-arrays are then sorted recursively. The pivot selection and partitioning continue until no more elements are left to partition.
Here are the Steps to implement Quick Sort in Java:
Choose the middle element as pivot.
Partition the array around the pivot so that every element in the left sub-array (L) is less than or equal to the pivot and every element in the right sub-array (R) is greater than the pivot.
Recursively apply the Quick Sort algorithm to the L and R sub-arrays.
implementation of Quick Sort in Java
Here is a simple implementation of Quick Sort in Java:
public class quick_sort {
public static void main(String[] args) {
int [] arr= {2,3,5,74,34,23};
sorting(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
static void sorting(int[] arr, int low, int high){
int start= low;
int end= high;
int pivot= start+(end-start)/2;
if(low>high){
return;
}
while(start<=end){
while(arr[start]<arr[pivot]){
start++;
}
while(arr[end]>arr[pivot]){
end--;
}
if(start<=end){
int temp=arr[start];
arr[start]= arr[end];
arr[end]= temp;
start++;
end--;
}
}
sorting(arr, low, end);
sorting(arr, start, high);
}
}
In the implementation, the quickSort method takes an input array, and then calls the Sorting method with the array's leftmost and rightmost indices. In the quickSort method, we first check if the left index is less than the right index, which implies the presence of two or more elements in the sub-array to be sorted.
In such cases, we select a pivot element and partition the array into two sub-arrays, with all elements less than or equal to the Pivot placed in one sub-array, and greater than the Pivot placed in the other sub-array. We then sort these smaller sub-arrays recursively using the same quickSort method.
Time and Space Complexity of Quick Sort
The worst-case time complexity of Quick Sort is O(n^2), where the array is already sorted in ascending or descending order. The best-case and average-case time complexity of Quick Sort is O(n * Log n), making it faster than most of the sorting algorithms that work with the same time complexity.
In terms of space complexity, Quick Sort's worst-case space complexity is O(n), which happens when the partition process is carried out in a way such that the minimum sub-array length is 1.
Conclusion
Quick Sort is a widely used sorting algorithm that provides fast and efficient sorting capabilities to developers. It works by recursively dividing the input array into smaller sub-arrays and swapping elements to put them in the correct order. With this implementation in Java, you have gained a better understanding of the algorithm and its implementation.




