Quick Sort Java

import org.testng.annotations.Test;

public class QuickSorts {

int []numbers;

publicvoid sort(int []values)

{

numbers=values;

for (int i=0;i<numbers.length;i++)

{

System.out.println(values[i]);

}

quicksort(0,numbers.length-1);

System.out.println(“Values after sort are”);

for(int i=0;i<numbers.length;i++)

{

System.out.println(numbers[i]);

}

}

public int [] quicksort(int low,int high)

{

int i=low,j=high;

int pivot=numbers[low+high/2];

while(i<=j)

{

while(numbers[i]<pivot)

i++;

while(numbers[j]>pivot )

j–;

if(i<=j)

{

int temp;

temp=numbers[i];

numbers[i]=numbers[j];

numbers[j]=temp;

i++;

j–;

}

}//i<=j

if(low<j)

quicksort(low,j);

if(i<high)

quicksort(i,high);

return numbers;

}

@Test

public void testSort() {

int arr[]={2,7,50,28};

sort(arr);

}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s