Showing posts with label quick sort in java. Show all posts
Showing posts with label quick sort in java. Show all posts

Wednesday, September 13, 2017

Quick Sort in Java Version 2

Here is the implementation of quick sort version 2 using Java as my programming language. Our program will ask the user how many items to be sorted and then it will ask the user to give a series of numbers and then it will be sorted using selection sort algorithm. I hope you will find my work useful. 


I am currently accepting programming work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

/**
 * 
 */
package quick_sort;

import java.util.Scanner;

/**
 * @author Jake R. Pomperada
 * Bacolod City, Negros Occidental, Philippines
 * September 13, 2017   Wednesday
*/


public class Quick_Sort_Demo {

/**
* @param args
*/
public static void Quick_Sort_All(int[] arr)
    {
        Quick_Sorting(arr, 0, arr.length - 1);
    }
    
    public static void Quick_Sorting(int arr[], int low, int high) 
    {
        int i = low, j = high;
        int temp;
        int pivot = arr[(low + high) / 2];

        
        while (i <= j) 
        {
            while (arr[i] < pivot)
                i++;
            while (arr[j] > pivot)
                j--;
            if (i <= j) 
            {
                /** swap **/
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;

                i++;
                j--;
            }
        }

        
        if (low < j)
        Quick_Sorting(arr, low, j);
        
        if (i < high)
        Quick_Sorting(arr, i, high);
    }
public static void main(String[] args) {
// TODO Auto-generated method stub
       Scanner input = new Scanner(System.in);
      
System.out.print("\n\n");
System.out.print("===== Quick Sort in Java ====");
System.out.print("\n\n");
System.out.print("===== Written By: Mr. Jake R. Pomperada =====");;
System.out.print("\n\n");
System.out.print("How many items? : ");
        int num = input.nextInt();
        int numbers[] = new int[num];

System.out.println();
        for (int i = 0; i < num; i++) {
            System.out.print ("Give value in item no. " + (i+1) + " : ");
            numbers[i] = input.nextInt();
        }

        System.out.print("\n\n");
System.out.print("Original Number Arrangements" );
System.out.print("\n\n");
        for (int temp : numbers){
        System.out.print(temp);
            System.out.print(", ");
        } 
        System.out.print("\n\n");
System.out.print("Sorted Number Arrangements" );
System.out.print("\n\n");
        
        
        Quick_Sort_All(numbers);
        for(int a:numbers){
            System.out.print(a);
            System.out.print(", ");
}
        input.close();

}

}





Friday, August 18, 2017

Quick Sort in Java

A very simple program that I wrote using Java to demonstrate quick sort algorithm. I hope you will find my work useful.  Thank you for all the support in my website.

I hope you will find my work useful and beneficial. If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.






Sample Program Output



Program Listing

QuickSort.java

package testing;

import java.util.Arrays;
import java.util.Scanner;

public class QuickSort {
public static void main(String[] args) {
 
Scanner input=new Scanner(System.in);

int x[]  =new int[5];
System.out.println();
System.out.println(" QUICK SORT IN JAVA ");
System.out.println();
for(int i=0;i<5;i++){
System.out.print("Give value in item no. " + (i+1) + " : ");
            x[i]=input.nextInt();
   }

System.out.println();
System.out.println("Original Values");
System.out.println();
System.out.println(Arrays.toString(x));

int low = 0;
int high = x.length - 1;

quickSort(x, low, high);
System.out.println();
System.out.println("Sorted Values");
System.out.println();
System.out.println(Arrays.toString(x));
input.close();
}

public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;

if (low >= high)
return;
 
int middle = low + (high - low) / 2;
int pivot = arr[middle];

int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}

while (arr[j] > pivot) {
j--;
}

if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
 
if (low < j)
quickSort(arr, low, j);

if (high > i)
quickSort(arr, i, high);
}

}