quicksorting with stacks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public void quicksort(int[] d, int lo, int hi){
	if(lo<hi){
		int pivotIndex=partition(d, lo, hi);
		quicksort(d,lo,pivotIndex-1);
		quicksort(d,pivotIndex+1,hi);
		}
	public int partition(int[] d,int lo, int hi){
		intStack smaller = new intStack();
		intStack larger = new intStack();
		int pivot = d[hi];
		for(int i=lo;i<hi;i++){
			if(d[i]<pivot)
				smaller.push(d[i]);
			else
				larger.push(d[i]);
		}
		int i=lo;
		while(!smaller.isEmpty()){
			d[i]=smaller.pop();
			i++;
		}
		d[i] = pivot;
		int pivotIndex = i;
		i++;
		while(!larger.isEmpty()){
			d[i] = larger.pop();
			i++;
		}
	return pivotIndex;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">