sorting - Did I understand the following quicksort algorithm correctly? -
i'm trying understand given algorithm , here thoughts:
a
given array... x
stands number on left side of pivot element, y
stands number on right side of pivot element. (let's pivot element element on right side of array.) , a[y] stands pivot element?
if understood correctly, algorithm first searches x
towards y
until first number greater or equal a[y]
, search y
towards x
until first number smaller or equal a[y]
. after, swap both numbers , repeat if i
hasn't reached j
. in end numbers left i
smaller a[y]
, numbers right j
larger a[y]
... move a[y]
middle. think this? right? maybe give example random array? cannot yet believe.
algorithm quicksort 1 func int div (a array; x, y integer) { 2 num = a[y]; 3 = x; 4 j = y-1; 5 repeat 6 while (a[i] <= num , < y) 7 = i+1; 8 end while; 9 while (a[j] >= num , j > x) 10 j = j-1; 11 end while; 12 if < j 13 swap (a[i], a[j]); 14 end if; 15 until >= j; 16 swap (a[i], a[y]); 17 return i; 18 }
from algorithm looks x , y mark left , right bounds of sorting algorithm within array (to sort complete array, you'd use x = 0 , y = a.length). pivot element rightmost 1 (at index y).
then, starts @ x (the left bound) , compares each element pivot a[y]. increases index, @ element larger a[y]. j starts @ y (the right bound) , complementary: decreases until reaches index, @ element smaller a[y] found.
if still smaller j, these 2 elements (at index , j) swapped, meaning elements x smaller a[y], elements j y larger a[y]. in case, there still elements between indices , j, have not been 'seen' algorithm. procedure repeated until eventually, complete array partitioned lower 'half' (all smaller a[y]) , upper 'half' (all larger a[y]). 1 element larger a[y] swapped a[y] , index between 2 partitions returned.
in conclusion, algorithm partitions array elements smaller a[y] , elements larger a[y]. repeating recursively on both partitions sort them completely.
example:
a = [ 4 7 9 2 5 1 3 8 6 ] --> called x = 0, y = 8 (thus, partitioning complete array) e.g. div([ 4 7 9 2 5 1 3 8 6 ], 0, 8) xi j y [ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, a[y]=6, i=0, j=7 x j y [ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, a[y]=6, i=1, j=7 a[i]=7 >= a[y]=6 -> first while loop ends x j y [ 4 7 9 2 5 1 3 8 | 6 ] x=0, y=8, a[y]=6, i=1, j=6 a[j]=3 <= a[y]=6 -> second while loop ends x j y [ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, a[y]=6, i=1, j=6 swapped a[i] , a[j] (i , j stay same!) -> repeat until i<j x j y [ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, a[y]=6, i=2, j=6 x j y [ 4 3 9 2 5 1 7 8 | 6 ] x=0, y=8, a[y]=6, i=2, j=5 x j y [ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, a[y]=6, i=2, j=5 swapped -> repeat (i<j) x j y [ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, a[y]=6, i=3, j=5 x j y [ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, a[y]=6, i=4, j=5 x ij y [ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, a[y]=6, i=5, j=5 first while ends again x j y [ 4 3 1 2 5 9 7 8 | 6 ] x=0, y=8, a[y]=6, i=5, j=4 second while ends again j>i -> don't swap j>i -> don't repeat swap a[i] , a[y] x j y [ 4 3 1 2 5 6 7 8 | 9 ] x=0, y=8, a[y]=6, i=5, j=4 return i=5 now, elements between x , smaller original a[y] (which different!) , elements between i+1 , y smaller original a[y]. next, you'd call div([ 4 3 1 2 5 6 7 8 9 ], 0, 5) , div([ 4 3 1 2 5 6 7 8 9 ], 6, 8) or more abstract: div(a, x, i) , div(a, i+1, y).
Comments
Post a Comment