Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, July 27, 2009

Quick Sort program in JAVA

Hi Friends,
Today I would like to give a program for Quick Sort.

package Quick_Sort;
import java.io.*;
import java.util.*;

public class quick_Sort {
static Scanner sc = new Scanner(System.in);
static int NodeValue = 0;
static int arrsize = 0;


static int getTotalElement(String str) {
System.out.println(str);
NodeValue = sc.nextInt();
return NodeValue;
}

static int getElement(String str) {
System.out.println(str);
int ret = sc.nextInt();
return ret;
}

static int putElement() {
return NodeValue;
}

public static void main(String[] args) {
int no_Elements = 0;
int i = 0;

no_Elements = getTotalElement("Enter the total number of elements :");
Element element1 = new Element();
int[] OriginalArray= element1.Elementarray;

for (i = 0; i < no_Elements; i++) {
int value = getElement("Enter the element " + i + " :");
element1.getElementId(value, i);
}
element1.getquicksort(OriginalArray,0,no_Elements-1);
element1.display();
}
}

class Element {
quick_Sort qs = new quick_Sort();
private int val = qs.putElement();
public int[] Elementarray = new int[val];

public void getElementId(int value, int val1) {
Elementarray[val1] = value;
}

public void getquicksort(int Elementarray[],int first,int last){
if (first {
int pivot=Elementarray[first];
int i=first;
int j=last;
while(i while(Elementarray[i]<=pivot && i i++;
while(Elementarray[j] >= pivot && j>first)
j--;
if(i swap(Elementarray,i,j);

}
swap(Elementarray,first,j);
getquicksort(Elementarray,first,j-1);
getquicksort(Elementarray,j+1,last);

}
}
void swap(int Elementarray[],int i,int j)
{
int temp;
temp=Elementarray[i];
Elementarray[i]=Elementarray[j];
Elementarray[j]=temp;
}
void display(){
System.out.println("The sorted list is :");
for(int i=0;i System.out.println(Elementarray[i]);
}
}

This program runs on jdk 1.5
versions less than jdk 1.5 are not compatible.
Hope my program was useful to you, please post your valuable feedback!
And your problems too. I hope to be of help when required.

No comments: