0% found this document useful (0 votes)
13 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Practical of

Data Structures – DSC - II

Submitted to: Submitted by:


Ms. Parul Gazta Abhishek
BU2019UGCS02
3rd Semester.
Aim: Write a Program to sort an array using Bubble Sort.
Code:
public class BubbleSort {
//implementation of bubble sort algorithm modified code
//time complexity is O(n^2)
public static void main(String[] args) {
int[] arr ={15,23,22,10,34,0,1};
BubbleSort bs = new BubbleSort();
bs.bubbleSort(arr);
System.out.println("Sorted Array:");
bs.printArray(arr);
}
void bubbleSort(int[] arr)
{
//modified code
int n = arr.length;
for(int i = 0; i< n -1; i++) //for passes
{
for (int j = 0; j< n-1-i; j++) //for element comparisons
{
if(arr[j]>arr[j+1])
{
//swapping will be done
int temp;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void printArray(int[] arr)
{
int n = arr.length;
for(int i = 0; i< n; i++)
{
System.out.print(arr[i]+" ");
}
}
}

Output:
Aim: Write a Program to sort an array using Insertion Sort.
Code:
public class InsertionSort {
public static void main(String[] args) {
int arr[] = {10, 1, 3, 12, 33, 90, 89};
int n = arr.length;
Insort is = new Insort();
is.InsSort(arr, n);
System.out.print("Sorted Arrays: ");
displays ds = new displays();
ds.display(arr, n);
}

static class Insort {


void InsSort(int arr[], int n) {
n = arr.length;
for (int i = 1; i < n; i++) {
int item;
item = arr[i];
int j; //j should be always one place behind i
for (j = i - 1; j >= 0 && arr[j] > item; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = item;
}
}
}
//to display or print Array
static class displays {
void display(int arr[], int n) {
n = arr.length;
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
}

Output:
Aim: Write a Program to sort an array using Selection Sort.
Code:
public class SelectionSort {
public static void main(String[] args) {
SelectionSort sb = new SelectionSort();
int arr[] = {75, 85, 19, 78, 90, 8};
int n = arr.length;
sb.SelSort(arr, n);
System.out.println("Sorted Arrays: ");
sb.printArray(arr, n);
}

void SelSort(int arr[], int n) {


//sorting logic of SelectionSort
n = arr.length;
for (int i = 0; i < n - 1; i++) {
//finding minimum element in unsorted arrays
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min])
min = j;

//swap the found element with first element


int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}

//print arrays
void printArray(int arr[], int n) {
n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output:
Aim: Write a Program to sort an array using Quick Sort.
Code:
public class QuickSortLast {
public static void main(String[] args) {
int[] arr = {70, 67, 11, 2, 3, 46};
int n = arr.length;
QuickSortLast qs = new QuickSortLast();
qs.sort(arr, 0, n - 1);
System.out.println("Sorted Arrays using Quick Sort: ");
qs.printArray(arr);
}

int partition(int[] arr, int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
//swapping of arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
//when for loop turns to false then swapping
//of arr[i+1] and arr[high] i.e pivot will be done
int temp = arr[i + 1];
arr[i + 1] = pivot;
arr[high] = temp;
return i + 1;
}

void sort(int[] arr, int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}

void printArray(int[] arr) {


int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:

Aim: Write a Program to sort an array using Merge Sort.


Code:
public class MergeSort {
public static void main(String[] args) {
int arr[] = {10, 8, 9, 88, 54, 3};
MergeSort ms = new MergeSort();
ms.sort(arr, 0, arr.length - 1);
System.out.print("Sorted arrays are: ");
ms.printArray(arr);

}
void merge(int arr[], int l, int m, int r) {
//finding size of two sub-arrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

//creating temporary arrays


int L[] = new int[n1];
int R[] = new int[n2];

//copy data to temp arrays


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];

//merge the temp arrays


//initial index of first and second array
int i = 0, j = 0;
//initial index of merged subarray

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++;
}
//copy remaining elements of L[] if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
//copy remaining element of R[] if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

//main function that sorts arr[l..r] using merge()


void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
//first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);

//merging method
merge(arr, l, m, r);
}
}

//method to print array


void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output:

You might also like