java - Find the k smallest integer in an array -



java - Find the k smallest integer in an array -

here code, works finding 1-7 smallest integers, 8 , 9. returns null when find 8 smallest integer in array. can help me problem? using quicksort here. much!

update: have figured out problem, array in main function. after alter next look,

int[] arr = {2, 3, 1, 7, 5, 6, 20, 8, 4, 9};

and

if(front>=end) homecoming input;

it works now!

import java.util.arrays; import java.io.*; class quicksort{ public static void main(string[] args){ int[] arr = new int[9]; arr[0] = 7; arr[1] = 2; arr[2] = 4; arr[3] = 8; arr[4] = 3; arr[5] = 5; arr[6] = 1; arr[7] = 0; arr[8] = 10; system.out.println((arrays.tostring(findksamllest(arr,8)))); } public static int partition(int[] input, int front, int end){ int pivot = input[front]; while(front < end){ while(input[front]<pivot) front++; while(input[end]>pivot) end--; swap(input,front,end); } homecoming front; } public static void swap(int[] input, int s, int l){ int temp = input[s]; input[s] = input[l]; input[l] = temp; } public static int[] findk(int[] input, int front, int end, int k){ if(front>=end) homecoming null; int pivot = partition(input,front,end); //system.out.println(pivot); if(k==pivot){ homecoming arrays.copyofrange(input,0,pivot); } else { if(k<pivot) homecoming findk(input,front,pivot,k); homecoming findk(input,pivot+1,end,k); } } public static int[] findksamllest(int[] input, int k){ homecoming findk(input, 0, input.length-1, k); }

}

change

if(front >= end) homecoming null;

to

if(front > end) homecoming null;

java arrays algorithm

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -