quick sort

quick sort

sir, i try to modify this one as u sugess me in previous answer "array based problem" for run time input.but i am facing some problem.plz solve this one also.

import java.util.*; public class QuickSort1 { public static void quick_srt(int array[],int low, int n){ int lo = low; int hi = n; if (lo >= n) { return; } int mid = array[(lo + hi) / 2]; while (lo < hi) { while (lo mid) { hi--; } if (lo < hi) { int T = array[lo]; array[lo] = array[hi]; array[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } quick_srt(array, low, lo); quick_srt(array, lo == low ? lo+1 : lo, n); }

public static void main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new int[10]; int i;

    System.out.println("       Quick Sort\n\n");   
    System.out.println("Values Before the sort:\n");    
    for(i = 0; i < array.length; i++){
    array[i]=input.nextInt();
     }
    for(i = 0; i < array.length; i++){
    System.out.println(array[i]);
        }



  quick_srt(array,0,array.length-1);
  System.out.print("Values after the sort:\n");    
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
   System.out.println();  

  System.out.println("PAUSE");

}

}

View Answers

January 5, 2011 at 10:13 AM

Hi Friend,

Here is required code:

import java.util.*;
public class QuickSort1 {
    public static void main(String a[]){
    int i;
    int array[] = new int[5];
    Scanner input=new Scanner(System.in);
    System.out.println("       Quick Sort\n\n");
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++){
     array[i]=input.nextInt();
    }
    quick_srt(array,0,array.length-1);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void quick_srt(int array[],int low, int n){
    int lo = low;
    int hi = n;
    if (lo >= n) {
      return;
    }
    int mid = array[(lo + hi) / 2];
    while (lo < hi) {
      while (lo<hi && array[lo] < mid) {
        lo++;
      }
      while (lo<hi && array[hi] > mid) {
        hi--;
      }
      if (lo < hi) {
        int T = array[lo];
        array[lo] = array[hi];
        array[hi] = T;
      }
    }
    if (hi < lo) {
      int T = hi;
      hi = lo;
      lo = T;
    }
    quick_srt(array, low, lo);
    quick_srt(array, lo == low ? lo+1 : lo, n);
  }
}

Thanks

Ads









Related Tutorials/Questions & Answers:
quick sort
; System.out.println(" Quick Sort\n\n"); System.out.println("Values Before...quick sort  sir, i try to modify this one as u sugess me in previous... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
quick sort
; System.out.println(" Quick Sort\n\n"); System.out.println("Values Before...quick sort  sir, i try to modify this one as u sugess me in previous... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
Advertisements
quick sort
; System.out.println(" Quick Sort\n\n"); System.out.println("Values Before...quick sort  sir, i try to modify this one as u sugess me in previous... static void quick_srt(int array[],int low, int n){ int lo = low; int hi
Quick Sort In Java
Quick Sort in Java      ... to sort integer values of an array using quick sort. Quick sort algorithm is developed by C. A. R. Hoare. Quick sort is a comparison sort. The working
Quick Sort in Java
Quick sort in Java is used to sort integer values of an array. It is a comparison sort. Quick sort is one of the fastest and simplest sorting algorithm... sort, etc. The complexity of quick sort in the average case is Θ(n log n
Quick Sort in Java
Quick Sort in Java Quick Sort in Java is used to sort elements of an array. Quick sort works on divide and conquer strategy and comparison sort... into two sub-arrays. The complexity of quick sort in the average case is &Theta
bubble sort
bubble sort  write a program in java using bubble sort
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  write a program in java using insertion sort
insertion sort
insertion sort  how many arrays needed for insertion sort and why
bubble sort
bubble sort  how to calculate the number of passes in bubble sort
buble sort
buble sort  ascending order by using Bubble sort programm   Java BubbleSort Example
SEARCH AND SORT
SEARCH AND SORT  Cam any one provide me the code in java that : Program to search for MAX,MIN and then SORT the set using any of the Divide and conquer method
Sort
Insertion Sort In Java
. There are more efficient algorithms such as quick sort, heap sort, or merge sort... Insertion Sort In Java     ... In this example we are going to sort integer values of an array using insertion sort
ModuleNotFoundError: No module named 'quick'
ModuleNotFoundError: No module named 'quick'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'quick' How to remove the ModuleNotFoundError: No module named 'quick'
Insertion Sort Applet
Insertion Sort Applet  Please All , I need Insertion sort applet program
Insertion Sort Applet
Insertion Sort Applet  I need Insertion Sort Applet code was design by Dr. Daniel Liang Please
insertion sort applet code
insertion sort applet code  i need Insertion Sort Applet Program
ModuleNotFoundError: No module named 'sort'
ModuleNotFoundError: No module named 'sort'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sort' How to remove the ModuleNotFoundError: No module named 'sort' error
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
visualization Max-Heap-sort
visualization Max-Heap-sort  To Whom It May Concern, Would you please give the source code of applet Max-heap sort? I want to see the visualization of Max-Heap sort. Best Regards, saeideh
Insertion Sort Timer
Insertion Sort Timer  Welcome all I wanna program in java find the timer of insertion sort and find time complexity for random value thanks all
Help With Costructing Selection sort?
Help With Costructing Selection sort?  Using a selection sort, for each entry in the array, display the original index of the first dimension... in advance!   Please visit the following link: Java Selection Sort
Version of com.codesnippets4all>quick-json dependency
List of Version of com.codesnippets4all>quick-json dependency

Ads