Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 05-17-09   #1 (permalink)
New to Overclock.net
 
adramalech707's Avatar
 
intel nvidia

Join Date: Feb 2008
Posts: 219

Rep: 5 adramalech707 Unknown
Unique Rep: 5
Folding Team Rank: 691
Trader Rating: 0
Default java quick sort....

okay so i have been assigned to do a quick sort algorthim solution for a problem of taking an array of any size and find kth largest.....where k is the size of the array...

pretty straight forward....but i am having some issues with my conquer algorthim (aka. finding the kth largest once i partition it)...

now since there is a couple different ways to setup the pivot i was instructed to make the pivot the right-most element and make the j the right_most element -1....then make i the left-most element and then as long as left pointer is less than pivot and right pointer is greater than pivot increase/decrease left and right....

if left is greater than pivot swap them then increase left visa versa with right... if right is lesser than pivot swap then decrease right...

at the end of that if the new left is greater than the new right swap them but don't increment them before checking those positions...

after everything is swapped and left and right pointers have crossed swap left pointer with the pivot and return the array and the pivot position...

that is the partition algorithm....

i am having some trouble with setting up i (left point) and j (right point) after i have partitioned...because the i and j are one away each direction of the position of the pivot so i reset them to the end and beginning accordingly...

but on the second partition it doesn't like this and crashes... i am going to maybe have too modify the array of the object where it will only show the partition... any suggest would be greatly appreciated...
here is some code:

Code:
import java.util.*;

public class Quick{
	private static final int MAX_SIZE = 10;
	private static int[] Array = new int[MAX_SIZE];
	private static int i = 0, j = 0, pivot = 0, tmp = 0;
	public static int size_left = 0, size_right = 0, overall_size = 0;
	
	public Quick(int[] input_nums, int left, int piv, int right){
		Array = input_nums;
		pivot = piv;
		i = left;
		j = right;
	}
	
	public static int find_kth_largest(Quick one, int largest){
		one = partition(one.Array, one.i, one.pivot, one.j);		
		size_left = one.pivot; 
		size_right = (MAX_SIZE -1) - one.pivot;
		
		one.i = one.pivot - size_left; 
		one.j = one.pivot + size_right;
		
		if(size_left == 0 && size_right == 0){ 
			return one.Array[one.pivot];
		}
		else {
			if(largest < size_right){//the largest is in the right side...
				one.i = one.pivot;
				one.pivot = one.i + size_right;
				one.j = one.pivot - 1;
				return find_kth_largest(one, largest);
			}
			else if(largest > size_right){
				if(largest == (size_right + 1)){
					one.j = one.pivot - 1;
				}
				else{
					one.pivot -= 1;
					one.j = one.pivot - 1;
				}
				largest -= size_right;
				return find_kth_largest(one, largest);
			}
			else{
				largest = size_right;
				one.i = one.pivot;
				one.pivot = one.pivot + size_right;
				one.j = one.pivot - 1;
				return find_kth_largest(one, largest);
			}
		}
	}
	
	public static Quick partition(int[] one, int left, int pivot, int right){
		if(left <= right){
			if(one[left] < one[pivot]){
				left++;
			}
			else{
				tmp = one[pivot];
				one[pivot] = one[left];
				one[left] = tmp;
				left++;
			}
			
			if(one[right] > one[pivot]){
				right--;
			}
			else{
				tmp = one[pivot];
				one[pivot] = one[right];
				one[right] = tmp;
				right--;
			}
			
			if((left < right) && (one[left] > one[right])){
				tmp = one[left];
				one[left] = one[right];
				one[right] = tmp;
			}
			return partition(one, left, pivot, right);
		}
		else{
			tmp = one[left];
			one[left] = one[pivot];
			one[pivot] = tmp;
			
			tmp = left;
			left = pivot;
			pivot = tmp;
			
			Quick Second = new Quick(one, left, pivot, right);
			return Second;
		}
	}
	
	public static String toString(int result, int largest){
		String output = "The " + largest + " largest is: " + result + ".";
		return output;
	}
	
	public static void main(String[] argc){
		String result = "";
		int number = 0, pos = 0, z = 0, tmp = 0;
		Scanner Keyboard = new Scanner(System.in);
		
		System.out.println("Please enter a number: ");
		number = Keyboard.nextInt();
		Array[pos++] = number;
	
		for(z = pos; z < MAX_SIZE; z++){
			System.out.println("Please enter another number: ");
			number = Keyboard.nextInt();
			Array[z] = number;
		}
		
		Quick First = new Quick(Array, 0, (MAX_SIZE-1), (MAX_SIZE-2));
		
		System.out.println("Please enter the number 1 - " + (MAX_SIZE-1) + "to find that largest in the list: ");
		number = Keyboard.nextInt();
		
		tmp = find_kth_largest(First, number);
		result = toString(tmp,number);
		
		System.out.println(result);
	}
}
__________________
x2 4600+ @2.6ghz

x2 6400+ @ 3.5ghz

Quote:
When you say "I wrote a program that crashed Windows", people just stare at you blankly and say "Hey, I got those with the system, *for free*"
-Linus Torvalds
Quote:
"How should I know if it works? That's what beta testers are for. I only coded it."
-Linus Torvalds
Quote:
Be warned that typing \fBkillall \fIname\fP may not have the desired effect on non-Linux systems, especially when done by a privileged user.
-killall man page

System: Intel build
CPU
i7 860 @ 2.8ghz
Motherboard
gigabyte p55-ud6
Memory
dominator gt CMG4GX3M2A1600C7
Graphics Card
bfg 9600gt oc2
Hard Drive
wd caviar 500gb
Power Supply
750watt toughpower
Case
antec 900
CPU cooling
coolermaster hyper tx3
GPU cooling
stock
OS
windows 7/ gentoo 64bit
Monitor
samsung 2243swx
adramalech707 is offline I fold for Overclock.net   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 11:03 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.10202 seconds with 8 queries