0% found this document useful (0 votes)
16 views4 pages

Merge Sort and Quick Sort

The document describes merge sort and quick sort algorithms. It includes Java code to implement the sorting of random arrays and measure the time taken using both algorithms.

Uploaded by

sunil gopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Merge Sort and Quick Sort

The document describes merge sort and quick sort algorithms. It includes Java code to implement the sorting of random arrays and measure the time taken using both algorithms.

Uploaded by

sunil gopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Merge sort

import java.util.*;

public class mergeSort {


public static void main(String args[]) {
mergeSort obj = new mergeSort();
Scanner sc = new Scanner(System.in);
int a[] = new int[10000];
long start, end;
System.out.println("Merge Sort");
System.out.println("Enter the size of array:");
int n = sc.nextInt();
for (int i = 0; i < n; i++)
a[i] = obj.generateRandom(a, 10000);
System.out.println("Before Sorting:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + "\t");
System.out.println();
start = System.nanoTime();
obj.sort(a, 0, n - 1);
end = System.nanoTime();
System.out.println("After Sorting");
for (int i = 0; i < n; i++)
System.out.print(a[i] + "\t");
System.out.println();
System.out.println("Time taken is" + "\t" + (end - start) + "ns");
sc.close();
}

public void sort(int a[], int low, int high) {


int mid;
if (low < high) {
mid = (low + high) / 2;
sort(a, low, mid);
sort(a, mid + 1, high);
combine(a, low, mid, high);
}
}

public void combine(int a[], int low, int mid, int high) {
int i, j, k;
int temp[] = new int[100];
i = low;
j = mid + 1;
k = low;
while ((i <= mid) && (j <= high)) {
if (a[i] <= a[j]) {
temp[k] = a[i];
i++;
} else {
temp[k] = a[j];
j++;
}
k++;
}
while (i <= mid) {
temp[k] = a[i];
i++;
k++;
}
while (j <= high) {
temp[k] = a[j];
j++;
k++;
}
for (int p = low; p <= high; p++) {
a[p] = temp[p];
}
}

public int generateRandom(int a[], int bound) {


Random r = new Random();
int offset = r.nextInt(bound);
while (alreadyThere(a, offset))
offset = r.nextInt(bound);
return offset;
}

private boolean alreadyThere(int arr[], int e) {


for (int i = 0; i < arr.length; i++) {
if (e == arr[i])
return true;
}
return false;
}
}

Quick sort

import java.util.*;

public class quickSort {


public static void main(String[] args) {

quickSort quick = new quickSort();


int a[] = new int[10000];
Scanner in = new Scanner(System.in);
long start, end;
System.out.println("QuickSort");
System.out.println("enter no. of elements");
int n = in.nextInt();
for (int i = 0; i < n; i++) {
a[i] = quick.generateRandom(a, 10000);
}
System.out.println("elements to be sorted are");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
start = System.nanoTime();
quicksort(a, 0, n - 1);
end = System.nanoTime();
System.out.println("the sorted elements are");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("time taken is" + "\t" + (end - start) + "ns");
System.out.println("--------");
in.close();
}

static void quicksort(int a[], int p, int q) {


int j;
if (p < q) {
j = partition(a, p, q);
quicksort(a, p, j - 1);
quicksort(a, j + 1, q);
}
}

static int partition(int a[], int m, int p) {


int v, i, j;
v = a[m];
i = m;
j = p;
while (i < j) {
while (a[i] <= v)
i++;
while (a[j] > v)
j--;
if (i < j)
interchange(a, i, j);
}
a[m] = a[j];
a[j] = v;
return j;
}

static void interchange(int a[], int i, int j) {


int p;
p = a[i];
a[i] = a[j];
a[j] = p;
}

public int generateRandom(int a[], int bound) {


Random r = new Random();
int offset = r.nextInt(bound);
while (alreadyThere(a, offset))
offset = r.nextInt(bound);
return offset;
}

private boolean alreadyThere(int arr[], int e) {


for (int i = 0; i < arr.length; i++) {
if (e == arr[i])
return true;
}
return false;
}
}

You might also like