The following article, Quick Sort in Java, provides an outline for the quick sort algorithm in java. The Quick Sort Algorithm is one of the sorting algorithms which is efficient and similar to that of the merge sort algorithm. This is one of the prevalently used algorithms for real-time sorting purposes. The worst-case time complexity of this algorithm is O(n^2), the average-case time complexity is O(n log n), and the best-case time complexity is O(n log n).
The space complexity if O(n log n), where is n is the size of the input. The process of sorting involves the partitioning of input, recursive iterations and marking a pivotal element for each recursion. The type of sorting in this algorithm involves a comparison of adjacent elements in an iterative manner.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
How Quick Sort Works in Java?
Quick Sort algorithm can be implemented in Java by forming a pseudo code with a sequence of steps designed and followed in an efficient manner.
- The main principle of the quick sort algorithm that it works is based on the divide and conquer approach and is also an efficient sorting algorithm.
- The input array is divided into sub-arrays, and the division is based on the pivot element, which is a central element. The sub-arrays on either side of the pivot element are the main areas where the sorting actually occurs.
- The central pivot element is the base to divide the array into two partitions where the left half of array elements are lesser than the pivot element, and the right half of array elements are greater than the pivot element.
- Before considering the pivot element, it can be anyone from the elements of an array. This is normally considered the middle one or the first one, or the last one for ease of understanding. The pivot element can be a random one from any of the array elements.
- In our example, the last element of an array is considered as a pivot element, where the partitioning of sub-arrays starts from the right end of the array.
- Finally, the pivot element will be in its actual sorted position after the completion of the sorting process, where the main process of sorting lies in the partition logic of the sorting algorithm.
- The efficiency of the algorithm depends on the size of the sub-arrays and how they are balanced. The more the sub-arrays are unbalanced, the more the time complexity will be, leading to worst-case complexity.
- The selection of pivot elements in a random manner results in the best time complexity in many cases instead of choosing a particular start, end or middle indexes as the pivot elements.
Examples to Implement Quick Sort in Java
The QuickSort algorithm has been implemented using Java programming language as below, and the output code has been displayed under the code.
- The code initially takes the input using the method quickSortAlgo() with the array, initial index and final index, i.e., length of the array as the arguments.
- After calling the quickSortAlgo() method, it checks if the initial index is less than the final index and then calls the arrayPartition() method to get the pivot element value.
- The partition element contains the logic of arranging the smaller and larger elements based on the element values around the pivot element.
- After getting the pivot element index after partition method execution, the quickSortAlgo() method is called by itself recursively until all the sub-arrays are partitioned and sorted completely.
- In the partition logic, the last element is assigned as a pivot element and the first element is compared with the pivot element, i.e. the last one where the elements are swapped based on whether they are smaller or greater.
- This process of recursion happens until all the elements of an array are partitioned and sorted, where the final result is a combined sorted array.
- The elements are swapped inside the for-loop iteration only in the case the element is lesser than or equal to the pivot element.
- After completing the iteration process, the last element is swapped, i.e. the pivot element value is moved to the left side so that the new partitions are made, and the same process repeats in the form of recursion, which results in series of sorting operations on different possible partitions as a formation of sub-arrays out of the given array elements.
- The below code can be run on any IDE, and the output can be verified by changing the array value in the main(). The main method is used just for the purpose of getting the output in the console. As a part of Java coding standards, the main method can be removed below, and an object can be created, and the below methods can be called by making them non-static.
Code Implementation of Quick Sort Algorithm in Java
Following is a code implementation:
Code:
/* * Quick Sort algorithm - Divide & Conquer approach */ public class QuickSortAlgorithm { public static void main(String[] args) { int[] array = { 99, 31, 1, 3, 5, 561, 1, 342, 345, 454 }; quickSortAlgo(array, 0, array.length - 1); for (int ar : array) { System.out.print(ar + " "); } } public static int arrayPartition(int[] array, int start, int end) { int pivot = array[end]; int i = (start - 1); for (int ele = start; ele <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500312462961.png?x-oss-process=image/resize,p_40" class="lazy" alt="Quick Sort in Java" ></p> <h3 id="Conclusion">Conclusion</h3> <p>The Quick Sort Algorithm is efficient but not much stable as compared to other sorting techniques. The efficiency of quick sort algorithms comes down in the case of a greater number of repeated elements which is a drawback. The space complexity is optimized in this quick sort algorithm.</p>
The above is the detailed content of Quick Sort in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),