Hi I am trying to put quicksort in my class like other methods and call it using the main method, I did it for Selection, Insertion, Bubble Sort and I need one more which is quicksort. I already made the calling code for quicksort but I don't know how to use quicksort in class.

I have the main method first then I have the classes

Main

/**ICS
 * @(#)sorting_problem_setting.java
 *
 * sorting_problem_setting application
 * author
 * James Samuel
 * 29/March/2011
 */


public class sorting_problem_setting {

    public static void main(String[] args) {
    	SortClass sl = new SortClass();

		// bubble sort
		int numbers[] = new int[10000];
		sl.makeData(numbers);
		sl.bubbleSort(numbers);
		sl.displayTen(numbers);

		// insertion sort
		int numbers2[] = new int[10000];
		sl.makeData(numbers2);
		sl.insertionSort(numbers2);
		sl.displayTen(numbers2);

		// selection sort
		int numbers3 [] = new int[10000];
		sl.makeData(numbers3);
		sl.selectionSort(numbers3);
		sl.displayTen(numbers3);

 		// quick sort
		int numbers3 [] = new int[10000];
		sl.makeData(numbers3);
		sl.selectionSort(numbers3);
		sl.displayTen(numbers3);
}
}

Class

//** 29/March/2011
//*
//** Author
//***** James Samuel
//** ICS


import java.util.*;
class SortClass {


		static public void makeData(int array[])
		{
			Random a = new Random();

			for (int ctr = 0; ctr < array.length; ctr++)
			{
				array[ctr] = Math.abs(a.nextInt()) % 999 + 1;
			}
		}

		static public int linearSearch (int numList [], int searchItem)
		{
			for (int ctr2=0; ctr2>numList.length;ctr2++)
			{
				if (searchItem==numList[ctr2])
				{
					return ctr2;
				}

			}
			return -1;
		}

		static public void displayTen (int array[])
		{
			for (int ctr = 0; ctr <100; ctr++)
			{
				System.out.print(array[ctr] + " ");
			}
			System.out.println("");
		}
//#########################################################################################
//Bubble sort
	static public void bubbleSort( int array2[] )
   {

      for ( int pass = 1; pass < array2.length; pass++ )
      {

         for ( int element = 0; element < array2.length - 1; element++ )
         {

            if ( array2[ element ] > array2[ element + 1 ] )
	     {
               swap( array2, element, element + 1 );
	     }

         }
      }

   }

   static public void swap( int array3[], int first, int second )
   {
      int hold;

      hold = array3[ first ];
      array3[ first ] = array3[ second ];
      array3[ second ] = hold;
   }
//#########################################################################################
//insertionSort
					public void insertionSort(int numList[])
					{
						int hold, ptr;
						for (int ctr = 1; ctr < numList.length; ctr++)
					{
						hold = numList[ctr];
						ptr = 0;
						while (hold > numList[ptr])
						{
							ptr++;
						}
						for (int count = ctr; count > ptr; count--)
						{
							numList[count] = numList[count - 1];
						}
						numList[ptr] = hold;
					}

					}

//#########################################################################################
	// Selection sort
			public void selectionSort(int array[]){


			for ( int ctr2 = 0; ctr2 < array.length; ctr2++)
			{
				for ( int ctr = ctr2; ctr < array.length; ctr++)
					{
						if (array[ctr2] > array [ctr])	
							 		{
										swap( array, ctr2, ctr );

							 		}

					}

			}



		 }	


}
//#########################################################################################


	//Quick Sort

Now I need a quicksort code in class so I can call it using the main method.
Can somebody help me or show me how to use quicksort.

Thanks in Advance

Add thess function in your sort class

public void quickSort(int array[]) 

{
	quickSort(array, 0, array.length - 1);              
}


public void quickSort(int array[], int start, int end)
{
     int i = start;                          
     int k = end;                            

     if (end - start >= 1)                   
     {
             int pivot = array[start];       
             while (k > i)                   
             {
                     while (array[i] <= pivot && i <= end && k > i)  
                             i++;                                    
                     while (array[k] > pivot && k >= start && k >= i) 
                         k--;                                        
                     if (k > i)                                       
                             swap(array, i, k);                      
             }
             swap(array, start, k);          
                                              
             quickSort(array, start, k - 1); 
             quickSort(array, k + 1, end);  
     }
     else    
     {
             return;                   
     }
}

Add this code to execute the sort from main

int numbers4 [] = new int[10000];
		sl.makeData(numbers4);
		sl.quickSort(numbers4);
		sl.displayTen(numbers4);
commented: thanks +0

Thanks, I did a different method but this works to, I just tried it. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.