QuickSort (With Code in Python/C++/Java/C) - Programiz

文章推薦指數: 80 %
投票人數:10人

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 //functiontoswapelements voidswap(int*a,int*b){ intt=*a; *a=*b; *b=t; } //functiontofindthepartitionposition intpartition(intarray[],intlow,inthigh){ //selecttherightmostelementaspivot intpivot=array[high]; //pointerforgreaterelement inti=(low-1); //traverseeachelementofthearray //comparethemwiththepivot for(intj=low;j usingnamespacestd; //functiontoswapelements voidswap(int*a,int*b){ intt=*a; *a=*b; *b=t; } //functiontoprintthearray voidprintArray(intarray[],intsize){ inti; for(i=0;i



請為這篇文章評分?