|
|
|
#1 (permalink) | |||||||||||||||
|
New to Overclock.net
|
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:
Quote:
Quote:
|
|||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|