Quick Sort Algorithm Program in java

Question:

Write a program to implement Quick Sort Algorithm.

Requirements :

public static void quickSort(int[] ar,int start,int end)

public static int partition(int[] ar,int start,int end)

Implement the main() inside the class : ‘QuickSort’

Sample Input and Output 1:

How many elements?8

Enter array elements:2

1

5

3

4

8

7

6

Array after sorting:1 2 3 4 5 6 7 8

Sample Input and Output 2:

How many elements?0

Invalid Input

Sample Input and Output 3:

How many elements?-1

Invalid Input

CODE:

import java.util.Scanner;
public class QuickSort
    { public static void main(String[] args)
        { int i,n;
        Scanner q= new Scanner(System.in);
          int []arr= new int[100];
          System.out.print("How many elements?");
          n=q.nextInt();
          if(n==0||n<0)
          { System.out.println("Invalid Input");}
          else
          {System.out.print("Enter array elements:");
          for(i=1;i<=n;i++)
          { arr[i]=q.nextInt();
          }
          quickSort(arr,1,n);
          System.out.print("Array after sorting:");
          for(i=1;i<=n;i++)
          System.out.print(arr[i]+" ");
        }}
    public static int partition(int a[],int beg,int end)
    { int left, right,temp,loc,flag;
      loc=left=beg;
      right=end;
      flag=0;
      while(flag!=1)
      { while((a[loc]<=a[right])&&(loc!=right))
        right--;
        if(loc==right)
        flag=1;
        else if(a[loc]>a[right])
        { temp=a[loc];
          a[loc]=a[right];
          a[right]=temp;
          loc=right;
        }
        if(flag!=1)
        { while((a[loc]>=a[left])&&(loc!=left))
          left++;
          if(loc==left)
          flag=1;
          else if(a[loc]<a[left])
          { temp=a[loc];
            a[loc]=a[left];
            a[left]=temp;
            loc=left;
          }
        }
      }
      return loc;
    }
static void quickSort(int a[], int beg, int end)
{ int loc;
  if(beg<end)
  { loc=partition(a,beg,end);
    quickSort(a,beg,loc-1);
    quickSort(a,loc+1,end);
  }
}
}


 data structures programs in java pdf | data structures programs in javatpoint | data structures course in java | data structures and algorithms programs in java |best data structures course in java | data structures programs using java | advanced data structures lab programs in java | data structures and algorithms course in java | basic data structure programs in java | c data structures programs | data structures example programs in java | data structures in java free course |graph data structure program in java | data structures using java | interview programs on data structures in java | linked list data structure program in java | data structure programs on java |

data structures in java online course | queue data structure program in java | data structure programming questions in java | data structures in java coding questions |
data structure coding interview questions java | stack data structure program in java | tree data structure programs in java | tree data structure code in java |
data structures and java | java with data structures programs | data structures programs pdf | codechef | google | amazon | 2021
Previous
Next Post »