954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trying to understand this code???

Hello

Im trying to understand and explain how the quick sort code actually works, i unable to understand or xplain line 4 to 11.
Can ne of u xplain to me briefly what those lines actually do in the code algorithm....
Apreciate ne help.

do {
while(strcmp(array[i].Genre,x)<0 && i<right) i++;       //line 2
while(strcmp(array[j].Genre,x)>0 && j>left) j--;	        //line 3

if(i<=j) {				                    //line 4
	temp = array[i];		                   //line 5
	array[i] = array[j];		                  //line 6
	array[j] = temp;		                  //line 7
	i++; j--;			                 //line 8
			
		}
	}while(i<=j);		                  //line 9
			
if(left<j) quick_sort(array, left, j);		     //line 10
if(i<right)quick_sort(array, i, right);		     //line 11
	}
nabil1983
Junior Poster in Training
73 posts since Mar 2005
Reputation Points: 13
Solved Threads: 0
 

Try reading up on quicksort in general... The loop (lines 2-9) make sure that all the low values are in one range and the high values in the other... The recursive calls (lines 10,11) then does the same for these fixed subsections, untill the trivial case of one or two elements..

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You