Any body could help to correct following code of quick sort would be appreciated.
When I run this code I found the items in the collection
are not sorted in ascending order.
//Quick Sort Algorithm -
/**
-
* Quick sort is a divide and conquer algorithm which relies on a partition
-
* operation: to partition an array an element called a pivot is selected.
-
* All elements smaller than the pivot are moved before it and all greater
-
* elements are moved after it. This can be done efficiently in linear time
-
* and in-place. The lesser and greater sublists are then recursively sorted.
-
* Efficient implementations of quick sort (with in-place partitioning) are
-
* typically unstable sorts and somewhat complex, but are among the fastest
-
* sorting algorithms in practice.
-
*
-
* @param vec as Vector of MyVector type.
-
* @param left of type int represent left end index of vector.
-
* @param right of type int represent right end index of vector.
-
*/
-
public static void quickSort(MyVector vec,int left,int right){
-
if(right-left+1<=10){
-
insertionSort(vec,left,right);
-
}
-
else{
-
medianOf3(vec,left,right);
-
int leftPtr=partition(vec,left,right);
-
quickSort(vec,left,leftPtr-1);
-
quickSort(vec,leftPtr+1,right);
-
}
-
}
-
-
-
/**
-
* medianOf3 helps to sort the elements at left,middle and right of vector.
-
*
-
* @param vec as Vector of MyVector type.
-
* @param left of type int represent left end index of vector.
-
* @param right of type int represent right end index of vector.
-
*/
-
public static void medianOf3(MyVector vec,int left,int right){
-
int middle = (left+right)/2;
-
if(((Comparable)vec.elementAt(left)).compareTo(vec.elementAt(middle))>0){
-
swap(vec,left,middle);
-
}
-
if(((Comparable)vec.elementAt(middle)).compareTo(right)>0){
-
swap(vec,middle,right);
-
}
-
if(((Comparable)vec.elementAt(left)).compareTo(vec.elementAt(middle))>0){
-
swap(vec,left,middle);
-
}
-
}
-
-
-
-
/**
-
* partition method help the do partition of vector into half where
-
* one half contains all the elements less than pivot element and other
-
* half contains all the elements greater than pivot element.
-
*
-
* @param vec as Vector of MyVector type.
-
* @param left of type int represent left end index of vector.
-
* @param right of type int represent right end index of vector.
-
* @return index of type int as partition point.
-
*/
-
public static int partition(MyVector vec,int left,int right){
-
Object pivot = vec.elementAt((left+right)/2);
-
while(true){
-
while(((Comparable)vec.elementAt(++left)).compareTo(pivot)<0);
-
while(((Comparable)vec.elementAt(--right)).compareTo(pivot)>0);
-
if(left>right){
-
break;
-
}
-
else{
-
swap(vec,left,right);
-
}
-
}
-
return left;
-
}
-
-
3 1734
where have you defined the class
as used in the code
- /**
-
* MyVector class contains all the methods which help to manipulate MyVector
-
* Object and it also implements Cloneable Interface in order to implement the
-
* clone() method.
-
* @author Sunil Tyata
-
* @version 2012.02.14
-
*/
-
public class MyVector implements Cloneable{
-
protected Object[] data;
-
protected int size;
-
private static final int INITIAL_CAPACITY=25;
-
-
-
/*
-
* Constructor for MyVector without andy parameter. But It initialize an
-
* Array of Object Class.
-
*/
-
public MyVector(){
-
data = new Object[INITIAL_CAPACITY];
-
size=0;
-
}
-
-
-
/*
-
* append method appending the element at the end of the vector
-
* @param as element of Object type to add on data array.
-
*/
-
public void append(Object element){
-
if(size==data.length)
-
expand();
-
data[size] = element;
-
size++;
-
}
-
-
-
/*
-
* clear method make the vector collection empty
-
*/
-
public void clear(){
-
for(int i=0;i<size;i++){
-
data[i] = null;
-
}
-
size = 0;
-
}
-
-
-
/*
-
* contains method check whether the vector contains the element
-
* @param as element of Object type to add on data array.
-
* @return of boolean type if data array contain element on it.
-
*/
-
public boolean contains(Object element){
-
for(int i=0;i<size;++i){
-
if(element.equals(data[i]))
-
return true;
-
}
-
return false;
-
}
-
-
-
/*
-
* elementAt method appending the element at the end of the vector
-
* @param as index of int type used to find the element in data array.
-
* @return of Object type as element from data array if exist.
-
*/
-
-
public Object elementAt(int index){
-
if(index<0 || index>=size)
-
return null;
-
return data[index];
-
}
-
-
-
-
/*
-
* indexOf method find the index of the element.
-
* @param as element of Object type used for finding index value.
-
* @return of int type as position of element in data array.
-
*/
-
-
public int indexOf(Object element){
-
for(int i=0;i<size;i++){
-
if(element.equals(data[i]))
-
return i;
-
}
-
return -1;
-
}
-
-
-
/*
-
* insertAt method inserts the element at the given index.
-
* @param as index of type int shows the position where to insert element
-
* @param as element of type Object which is going to insert in data array.
-
* @return of boolean type which makes sure that insertion is successful.
-
*/
-
public boolean insertAt(int index, Object element){
-
if(index<0 || index>=size)
-
return false;
-
if(size==data.length)
-
expand();
-
for(int i=size;i>index;i--){
-
data[i] = data[i-1];
-
}
-
data[index] = element;
-
++size;
-
return true;
-
}
-
-
-
/*
-
* isEmpty method check whether the vector is empty.
-
* @return of boolean type which makes sure that vector is empty.
-
*/
-
public boolean isEmpty(){
-
return size<=0;
-
}
-
-
-
/*
-
* expand method helps of expand the size of fixed array of arbitrary size.
-
*/
-
private void expand(){
-
Object[] temp = new Object[2*data.length];
-
for(int i=0;i<size;i++){
-
temp[i] = data[i];
-
}
-
-
data = temp;//temp is just a temporary object type array
-
//temp will disappare after the completion of expand method
-
//here data = temp : temp is reffered to data here.
-
}
-
-
-
-
/*
-
* removeAt method remove the element at the given index.
-
* @param as index of type int shows the position of element
-
* @return as Object type which is the element to be removed.
-
*/
-
public Object removeAt(int index){
-
if(index<0 || index>=size)
-
return null;
-
Object o = data[index];
-
for(int i=index;i<size-1;++i){
-
data[i]=data[i+1];
-
}
-
--size;
-
return o;
-
}
-
-
-
/*
-
* remove method remove the element from the vector.
-
* @param as element of type Object.
-
* @return as boolean type which indicates element has been removed.
-
*/
-
public boolean remove(Object element){
-
return removeAt(indexOf(element))!=null;
-
}
-
-
-
/*
-
* replace method replace the element at the given index with the given
-
* element.
-
* @param as index of type int shows the position where to replace element
-
* @param as element of type Object which is going to insert in data array.
-
* @return of boolean type which makes sure that replacement is successful.
-
*/
-
public boolean replace(int index,Object element){
-
if(index<0 || index>=size)
-
return false;
-
data[index] = element;
-
return true;
-
}
-
-
-
/*
-
* size method get the number of elements in the current vector.
-
* @return as int type indicating size of vector.
-
*/
-
public int size(){
-
return size;
-
}//size() – get the number of elements in the current vector
-
-
-
/*
-
* ensureCapacity method make sure the vector gets at least the given
-
* capacity element.
-
* @param as minCapacity of type int used to check capacity.
-
*/
-
public void ensureCapacity(int minCapacity){
-
if(minCapacity<=data.length)
-
return;
-
Object[] temp = new Object[minCapacity];
-
data = temp;//this really imp line where temp disappear at the end of the method.
-
//but befor that temp is referred by data.
-
}
-
-
-
/*
-
* clone method returns a cloned copy of this vector.
-
* @return as MyVector type which is actually a clone of original vector.
-
*/
-
public MyVector clone(){
-
MyVector vecCopy = new MyVector();
-
vecCopy.ensureCapacity(this.size);
-
-
for(int i=0;i<size;++i){
-
vecCopy.data[i] = this.data[i];
-
}
-
vecCopy.size = this.size;
-
return vecCopy;
-
}
-
-
-
/*
-
* returns a string representation of this vector, containing the String
-
* representation of each element. Actually, toString() method overrides
-
* the toString() method of Object class.
-
* @return as String type comprises of different information like size,
-
* capacity and all the elements of vector.
-
*/
-
public String toString(){
-
-
String str = "+++...+++\n"
-
+"the current vector contain the following\n";
-
str+="Size = "+size+"\n";
-
str+="capacity = "+data.length+"\n";
-
-
for(int i=0;i<size;++i){
-
-
str+=i+":"+data[i]+"\t";
-
if((i+1)%5==0)
-
str+="\n";
-
}
-
-
str+="+++...+++";
-
return str;
-
-
}
-
-
-
/*
-
* removeRange method removes from this vector all of the elements whose
-
* index is between fromIndex, inclusive and toIndex, exclusive
-
* @param as fromIndex of type int shows the position where to start.
-
* @param as toIndex of type int shows the position where to end.
-
*/
-
public void removeRange(int fromIndex,int toIndex){
-
if(fromIndex>=toIndex)
-
return;
-
if(fromIndex<0)
-
fromIndex =0;
-
if(toIndex>=size)
-
toIndex = size;
-
-
int num=toIndex-fromIndex;
-
-
for(int i=fromIndex;i<size-num;++i){
-
data[i]=data[i+num];
-
}
-
-
for(int j=size-num;j<size;++j)
-
data[j] = null;
-
size = size-num;
-
}
-
-
-
/*
-
* reverse method removes from this vector all of the elements whose
-
* index is between fromIndex, inclusive and toIndex, exclusive
-
*/
-
public void reverse(){
-
for(int i=0,j=size-1;j>i;j--,i++){
-
Object tempElement = data[i];
-
data[i] = data[j];
-
data[j] = tempElement;
-
}
-
}
-
-
-
/*
-
* add all the elements in vector2 to the end of this vector
-
* @param as vector2 of MyVector type which is one of the part of mering
-
* vector.
-
*/
-
public void merge(MyVector vector2){
-
for(int i=0;i<vector2.size;++i){
-
this.append(vector2.data[i]);
-
}
-
}
-
-
}
-
I used above MyVector class for other different sorting algrithms they worked well I thing the error must be within quickSort method. Thank you. Hoping for ultimate answer.
I hope you got my reply.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Eva |
last post by:
Hi,
I try to implement quick sort. I sort vectors by their first value.
10 2 3 4
9 3 5 6
10 4 5 6
must be
9 3 5 6
10 2 3 4
10 4 5 6
The prog works great on maybe 500 vectors, but I have an...
|
by: TrvlOrm |
last post by:
HI There,
I have been struggling with JavaScript code for days now, and this is
my last resort! Please help...
I am trying to create a JavaScript slide show with links for Next
Slide,...
|
by: Mark Kamoski |
last post by:
Hi Everyone--
Please help.
I need a code sample for quick sort.
Thank you.
--Mark
|
by: Frank King |
last post by:
Hi,
I am using CArray and quick sort funciton to sort an array
of double type of data points. I found an article in MSDN
HOWTO: Quick Sorting Using MFC CArray-Derived Classes
ID: Q216858
...
|
by: ralphedge |
last post by:
These sorts work fine on 100000 ints but if I go much higher they will
both segmentation fault
**************************MERGESORT*********************
mergesort(int *a, int size) //a is...
|
by: AngelLopez1989 |
last post by:
I need the complete C++ program/ algorithm of Quick Sort. Can you please help me out? No pseudocode please. Can you please also explain how to do the quick sort? Thank you!
|
by: needhelp123 |
last post by:
Can any one send me a quick sort simple logic pogram...
its very urgent urgent
|
by: neehakale |
last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort.
I got an idea,in which situation we can use...
|
by: Rerun05 |
last post by:
I have a quick sort that reads in data from a file. I know there is something wrong with the partition part of my quick sort and i know exactly what line of code it is. I keep getting the Error...
|
by: sophia.agnes |
last post by:
Dear all,
The following is the quick sort function which i have seen in my note
book
how good is this function ?
void sort(int a, int begin, int end)
{
int pivot,l,r;
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |