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

Dsa File

The documents discuss implementations of selection sort, bubble sort, insertion sort, and merge sort algorithms in Java. Code snippets are provided to sort arrays of integers using each of the sorting techniques.

Uploaded by

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

Dsa File

The documents discuss implementations of selection sort, bubble sort, insertion sort, and merge sort algorithms in Java. Code snippets are provided to sort arrays of integers using each of the sorting techniques.

Uploaded by

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

Q. WAP in JAVA to implement Selection sort .

import java.util.Scanner;

public class Selection_Sort


{
void sort(int arr[])
{
int n = arr.length;

for (int i = 0; i < n-1; i++)


{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

int temp = arr[min_idx];


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

public static void main(String args[])


{
Selection_Sort ob = new Selection_Sort();
int arr[] = {15,5,19,28,11};
ob.sort(arr);
System.out.println("Sorted array: ");
ob.printArray(arr);
}
}

OUTPUT

Sorted array:
5 11 15 19 28
Q. WAP in JAVA to implement Bubble sort .

import java.util.Scanner;
class Bubble_sort {

static void bubbleSort(int arr[], int n)


{
int i, j, temp;
boolean swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {

temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}

if (swapped == false)
break;
}
}
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
OUTPUT

Sorted array:
11 12 22 25 34 64 90
Q. WAP in JAVA to implement insertion sort .

import java.util.Scanner;

class insertion_sort{
// Sorting Algorithm applied
public int[] fxn(int ar[],int n){
for(int i=0;i<n-1;i++){
int it =ar[i+1]; // it = iteration
int j = i;
while(j>=0 && ar[j]>it){
ar[j+1] = ar[j];
j--;
}
ar[j+1] = it;
}
return ar;
}
}

public class Insertion_sort{


public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements: ");
int n = sc.nextInt();
int ar[] = new int[n];
System.out.print("Enter element: ");
for(int i=0;i<n;i++)
ar[i] = sc.nextInt();

insertion_sort obj = new insertion_sort();


int br[] = obj.fxn(ar,n);
System.out.print("Sorted Array: ");
for(int i=0;i<n;i++)
System.out.println(br[i] + " ");

sc.close();

}
}
OUTPUT

Enter the number of elements:

Enter element:

42719

Sorted Array:

9
Q. WAP in JAVA to implement merge sort .

import java.util.Scanner;

class MergeSort {

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++;
}
}

void sort(int arr[], int l, int r)


{
if (l < r) {

int m = l + (r - l) / 2;

sort(arr, l, m);
sort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

static void printArray(int arr[])


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

public static void main(String args[])


{
int arr[] = { 12, 11, 13, 5, 6, 7 };
System.out.println("Given array is");
printArray(arr);

MergeSort ob = new MergeSort();


ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array is");


printArray(arr);
}
}
OUTPUT

Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13

You might also like