Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 3rd, 2006, 11:50 PM   #1
xenop
Newbie
 
Join Date: Jan 2006
Location: Lancaster, California
Posts: 11
Rep Power: 0 xenop is on a distinguished road
Send a message via AIM to xenop Send a message via Yahoo to xenop
Kth smallest array element

Im trying to write a program to find the smalles interger that belongs in the kth element of an array. Im having trouble seeing what im doing wrong and was hoping some one could help me out.Thank You

public int kSmallest(int k) {
			
			return kSmallest(k-1, 0, num.length-1);
			}
			private int kSmallest(int k, int left, int right){
				int rtn;
				int pivot;
				int pivotIndex;
				
				if (right <= left)
					rtn = num[left];
				else {//partition
				  
					pivot = num[(int)     Math.random()*(right-left)+left];
					pivotIndex = partition(pivot, left, right);
					System.out.println("pivot = "+ pivot);
						
			
				if (k < pivotIndex)
				kSmallest(k, left, pivotIndex-1);
				else
					kSmallest(k, pivotIndex+1, right );
				
				rtn = num[k];
				}
				
				return rtn;
			
			}

			private int partition(int pivot, int left, int right){
			
				while (left < right){
					while (num[left] < pivot) left++;
					while (num[right] > pivot) right--;
					exch(left,right);
				}
			return left;
			}
			private void exch( int i, int j) {
		     
		        int swap = num[i];
		        num[i] = num[j];
		        num[j] = swap;
		    }

Last edited by xenop; Nov 4th, 2006 at 12:01 AM.
xenop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
acessing array element edemkay Assembly 1 Apr 5th, 2006 7:05 AM
acessing array element edemkay Assembly 0 Apr 5th, 2006 2:24 AM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 9:19 PM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC