QuickSort (With Code in Python/C++/Java/C) - Programiz
文章推薦指數: 80 %
Quicksort is an algorithm based on divide and conquer approach in which an array is split into sub-arrays and these sub arrays are recursively sorted to get ...
CourseIndex
ExploreProgramiz
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
QuicksortAlgorithm
MergeSortAlgorithm
LinkedListDataStructure
HashTableDataStructure
DynamicProgramming
StartLearningDSA
LearningPaths
Challenges
LearnPythonInteractively
TryforFree
Courses
BecomeaPythonMaster
BecomeaCMaster
BecomeaJavaMaster
ViewallCourses
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
QuicksortAlgorithm
MergeSortAlgorithm
LinkedListDataStructure
HashTableDataStructure
DynamicProgramming
StartLearningDSA
AllDSATutorials
Python
JavaScript
C
C++
Java
Kotlin
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
AllPythonExamples
DSAIntroduction
Whatisanalgorithm?
DataStructureandTypes
WhylearnDSA?
AsymptoticNotations
MasterTheorem
DivideandConquerAlgorithm
DataStructures(I)
Stack
Queue
TypesofQueue
CircularQueue
PriorityQueue
Deque
DataStructures(II)
LinkedList
LinkedListOperations
TypesofLinkedList
HashTable
HeapDataStructure
FibonacciHeap
DecreaseKeyandDeleteNodeOperationsonaFibonacciHeap
TreebasedDSA(I)
TreeDataStructure
TreeTraversal
BinaryTree
FullBinaryTree
PerfectBinaryTree
CompleteBinaryTree
BalancedBinaryTree
BinarySearchTree
AVLTree
TreebasedDSA(II)
BTree
InsertioninaB-tree
DeletionfromaB-tree
B+Tree
InsertiononaB+Tree
DeletionfromaB+Tree
Red-BlackTree
Red-BlackTreeInsertion
Red-BlackTreeDeletion
GraphbasedDSA
GraphDataStructure
SpanningTree
StronglyConnectedComponents
AdjacencyMatrix
AdjacencyList
DFSAlgorithm
Breadth-firstSearch
BellmanFord'sAlgorithm
SortingandSearchingAlgorithms
BubbleSort
SelectionSort
InsertionSort
MergeSort
Quicksort
CountingSort
RadixSort
BucketSort
HeapSort
ShellSort
LinearSearch
BinarySearch
GreedyAlgorithms
GreedyAlgorithm
Ford-FulkersonAlgorithm
Dijkstra'sAlgorithm
Kruskal'sAlgorithm
Prim'sAlgorithm
HuffmanCoding
DynamicProgramming
DynamicProgramming
Floyd-WarshallAlgorithm
LongestCommonSequence
OtherAlgorithms
BacktrackingAlgorithm
Rabin-KarpAlgorithm
RelatedTopics
InsertionSortAlgorithm
SelectionSortAlgorithm
BubbleSort
BinarySearch
CountingSortAlgorithm
ShellSortAlgorithm
QuicksortAlgorithm
Inthistutorial,youwilllearnaboutthequicksortalgorithmanditsimplementationinPython,Java,C,andC++.
Quicksortisasortingalgorithmbasedonthedivideandconquerapproachwhere
Anarrayisdividedintosubarraysbyselectingapivotelement(elementselectedfromthearray).
Whiledividingthearray,thepivotelementshouldbepositionedinsuchawaythatelementslessthanpivotarekeptontheleftsideandelementsgreaterthanpivotareontherightsideofthepivot.
Theleftandrightsubarraysarealsodividedusingthesameapproach.Thisprocesscontinuesuntileachsubarraycontainsasingleelement.
Atthispoint,elementsarealreadysorted.Finally,elementsarecombinedtoformasortedarray.
WorkingofQuicksortAlgorithm
1.SelectthePivotElement
Therearedifferentvariationsofquicksortwherethepivotelementisselectedfromdifferentpositions.Here,wewillbeselectingtherightmostelementofthearrayasthepivotelement.
Selectapivotelement2.RearrangetheArray
Nowtheelementsofthearrayarerearrangedsothatelementsthataresmallerthanthepivotareputontheleftandtheelementsgreaterthanthepivotareputontheright.
PutallthesmallerelementsontheleftandgreaterontherightofpivotelementHere'showwerearrangethearray:
Apointerisfixedatthepivotelement.Thepivotelementiscomparedwiththeelementsbeginningfromthefirstindex.
Comparisonofpivotelementwithelementbeginningfromthefirstindex
Iftheelementisgreaterthanthepivotelement,asecondpointerissetforthatelement.
Iftheelementisgreaterthanthepivotelement,asecondpointerissetforthatelement.
Now,pivotiscomparedwithotherelements.Ifanelementsmallerthanthepivotelementisreached,thesmallerelementisswappedwiththegreaterelementfoundearlier.
Pivotiscomparedwithotherelements.
Again,theprocessisrepeatedtosetthenextgreaterelementasthesecondpointer.And,swapitwithanothersmallerelement.
Theprocessisrepeatedtosetthenextgreaterelementasthesecondpointer.
Theprocessgoesonuntilthesecondlastelementisreached.
Theprocessgoesonuntilthesecondlastelementisreached.
Finally,thepivotelementisswappedwiththesecondpointer.
Finally,thepivotelementisswappedwiththesecondpointer.
3.DivideSubarrays
Pivotelementsareagainchosenfortheleftandtherightsub-partsseparately.And,step2isrepeated.
Selectpivotelementofineachhalfandputatcorrectplaceusingrecursion
Thesubarraysaredivideduntileachsubarrayisformedofasingleelement.Atthispoint,thearrayisalreadysorted.
QuickSortAlgorithm
quickSort(array,leftmostIndex,rightmostIndex)
if(leftmostIndex
延伸文章資訊
- 1Comparison Sort: Quick Sort(快速排序法)
Quick Sort, Merge Sort, Heap Sort, Insertion Sort, Selection Sort. best case, NlogN, NlogN, NlogN...
- 2快速排序Quicksort
Quicksort 是一個非常熱門且應用廣泛的排序法,相對簡單的實作就可達到O(nlogn) 的平均時間複雜度。雖然最差時間複雜度與bubble sort 同為O(n2),但這種情形非常少見。
- 3[演算法] 快速排序法(Quick Sort) - iT 邦幫忙
快速排序(Quick Sort) 的想法是說,先找一個基準點,然後派兩個代理人分別從資料的兩邊開始往中間找,如果右邊找到一個值比基準點小,左邊找到一個值比基準點大,就讓 ...
- 4[演算法] 快速排序法(Quick Sort)
[演算法(Algorithm)] 快速排序法(Quick Sort) · 選定一個基準值(Pivot) · 將比基準值(Pivot)小的數值移到基準值左邊,形成左子串列 · 將比基準值(Pivo...
- 5【TBS Learning】演算法-六種排序法之五:快速排序法(quick sort)