You are on page 1of 6

LAB # 05

Sorting on Linear Array

Object:

To sort a linear array using Selection Sort, Bubble Sort and Merge Sort.

Lab Tasks:

1. Write a program for Selection sort that sorts an array containing numbers, prints
all the sort values of array each followed by its location.

Code:

package Lab5;
import
java.util.*;

public class Task1 {


public static void main(String[] args){
int[] arr = {8,2,6,10,0,45,3};
System.out.println("Array before Sorting:
"+Arrays.toString(arr));
selectionSort(arr);
System.out.println("After Sorting: ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("[%d] = %d \n",i,arr[i]);
}
}

public static void selectionSort(int[] arr){


for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
}
Output

2. Write a program that takes 10 numbers as input in an array. Sort the elements of
array by using Bubble sort. Print each iteration of the sorting process.

Code:
package Lab5;
import java.util.*;

public class Task2 {


public static void main(String[] args) {
int[] arr = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i <arr.length; i++) {
System.out.print("Enter any number: ");
arr[i] = sc.nextInt();
}
bubbleSort(arr);
System.out.println("\nArray After Sort:"+Arrays.toString(arr));
}
public static void bubbleSort(int[] arr){
for(int i=arr.length-1; i>0; i--){
System.out.println("\nPass "+(arr.length-i)+":");
for (int j=0; j<i; j++){
if(arr[j]>arr[j+1]){ int temp = arr[j];
arr[j] = arr[j+1]; arr[j+1] = temp;
System.out.println(Arrays.toString(arr));
}
}
}
}
}
OUTPUT:

3. Write a program that takes 10 random numbers in an array. Sort the elements of
array by using Merge sort. Print each iteration of the sorting process.

Code:
package Lab5;
import java.util.*;

public class Task3 {


public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[10];
for (int i=0; i<arr.length; i++){
arr[i] = r.nextInt(100);
}
System.out.println("Iteration Steps: ");
mergeSort(arr,0,arr.length-1);

public static void merge(int arr[], int l, int m, int r)


{
int n1 = m - l + 1;
int n2 = r - m;

int L[] = new int[n1];


int R[] = new int[n2];
for (int i = 0; i < n1; ++i) L[i] = arr[l + i];
for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j];

int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i]; i++;
}
else {
arr[k] = R[j]; j++;
} k++;
}

while (i < n1) { arr[k] = L[i]; i++;


k++;
}
while (j < n2) { arr[k] = R[j]; j++;
k++;
}
}

public static void mergeSort(int arr[], int l, int r)


{
if (l < r) {
// Find the middle point
int m =l+ (r-l)/2;
// Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m + 1, r);
// Merge the sorted halves merge(arr, l, m, r);
System.out.println(Arrays.toString(arr));

}
}
}

OUTPUT:

Home Task:

1. Declare an array of size n to store account balances. Initialize with values 0 to


1000000 and sort Account No’s according to highest balance values by using
Quick sort, For e.g.:
Account No. 3547 Balance 28000
Account No. 1245 Balance 12000
Code:
package Lab5; import java.util.*;

public class HomeTask1 {


public static void main(String[] args) {
int[] arr = {3000,200,50000,60000,770000};
quickSort(arr,0,arr.length-1);
for (int i = arr.length-1;i>=0; i--){ System.out.printf("Account No %d Balance:
%d\n",i+1,arr[i]);
}
}
public static void swap(int[] arr, int i, int j)
{
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}

public static int partition(int[] arr, int low, int high){ int pivot =
arr[high];

int i = (low - 1);

for(int j = low; j <= high - 1; j++)


{
if (arr[j] < pivot){ i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; return (i + 1);
}

public static void quickSort(int[] arr, int low, int high){ if (low < high){
int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr,
pi + 1, high);
}
}

OUTPUT:

You might also like