Write A Java Program To Implement Quick Sort of Given Elements
Write A Java Program To Implement Quick Sort of Given Elements
Write A Java Program To Implement Quick Sort of Given Elements
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
temp = array[low];
array[low] = array[x];
array[x] = temp;
temp = array[j];
array[j] = array[i];
array[i++] = temp;
i++;
}
temp = array[i - 1];
array[i - 1] = array[low];
array[low] = temp;
return i - 1;
if(low<high){
quickSort(array,low,mid-1);
quickSort(array,mid+1,high);
int size;
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
int i;
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println(Arrays.toString(array));
quickSort(array,0,array.length-1);
System.out.println(Arrays.toString(array));
public AVLNode()
{
left = null;
right = null;
data = 0;
height = 0;
}
public AVLNode(int n)
{
left = null;
right = null;
data = n;
height = 0;
}
}
class AVLTree
{
private AVLNode root;
/* Constructor */
public AVLTree()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Make the tree logically empty */
public void makeEmpty()
{
root = null;
}
/* Function to insert data */
public void insert(int data)
{
root = insert(data, root);
}
/* Function to get height of node */
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
/* Function to max of left/right node */
private int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}
/* Function to insert data recursively */
private AVLNode insert(int x, AVLNode t)
{
if (t == null)
t = new AVLNode(x);
else if (x < t.data)
{
t.left = insert( x, t.left );
if( height( t.left ) - height( t.right ) == 2 )
if( x < t.left.data )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x > t.data )
{
t.right = insert( x, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( x > t.right.data)
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
/* Rotate binary tree node with left child */
private AVLNode rotateWithLeftChild(AVLNode k2)
{
AVLNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
Output:
AVLTree Tree Test
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
4
Empty status = true
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
AVLTree Operations
1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
10
Post order : 10
Pre order : 10
In order : 10