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

Dsa Codes

The document contains code implementations for several sorting algorithms: 1) Binary search, linear search, and radix sort are searching/sorting algorithms that are described and implemented. 2) Merge sort, quick sort, and insertion sort are sorting algorithms that are described and implemented using recursive functions to divide and combine sub-problems. 3) The code provides main methods to test each algorithm on sample input arrays.

Uploaded by

adkal462
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)
36 views

Dsa Codes

The document contains code implementations for several sorting algorithms: 1) Binary search, linear search, and radix sort are searching/sorting algorithms that are described and implemented. 2) Merge sort, quick sort, and insertion sort are sorting algorithms that are described and implemented using recursive functions to divide and combine sub-problems. 3) The code provides main methods to test each algorithm on sample input arrays.

Uploaded by

adkal462
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/ 8

DSA CODES

BINARY SEARCH:
class BinarySearch {

int binarySearch(int arr[], int x) {

int l = 0, r = arr.length - 1;

while (l <= r) {

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

if (arr[m] == x)

return m;

if (arr[m] < x)

l = m + 1;

else

r = m - 1;

return -1;

public static void main(String args[]) {

BinarySearch ob = new BinarySearch();

int arr[] = { 2, 3, 4, 10, 40 };

int n = arr.length;

int x = 10;

int result = ob.binarySearch(arr, x);

if (result == -1)

System.out.println("Element not present");

else

System.out.println("Element found at index " + result);

}
}

LINEAR SEARCH:
public class LinearSearch {

public static int search(int arr[], int x) {

int n = arr.length;

for (int i = 0; i < n; i++) {

if (arr[i] == x)

return i;

return -1;

public static void main(String args[]) {

int arr[] = { 2, 3, 4, 10, 40 };

int x = 10;

int result = search(arr, x);

if (result == -1)

System.out.print("Element is not present in array");

else

System.out.print("Element is present at index " + result + "\n");

RADIX SORT:
import java.io.*;

import java.util.*;

class Radix {
static int getMax(int arr[], int n) {

int mx = arr[0];

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

if (arr[i] > mx)

mx = arr[i];

return mx;

static void countSort(int arr[], int n, int exp) {

int output[] = new int[n];

int i;

int count[] = new int[10];

Arrays.fill(count, 0);

for (i = 0; i < n; i++)

count[(arr[i] / exp) % 10]++;

for (i = 1; i < 10; i++)

count[i] += count[i - 1];

for (i = n - 1; i >= 0; i--) {

output[count[(arr[i] / exp) % 10] - 1] = arr[i];

count[(arr[i] / exp) % 10]--;

for (i = 0; i < n; i++)

arr[i] = output[i];

}
static void radixsort(int arr[], int n) {

int m = getMax(arr, n);

for (int exp = 1; m / exp > 0; exp *= 10)

countSort(arr, n, exp);

static void print(int arr[], int n) {

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

System.out.print(arr[i] + " ");

public static void main(String[] args) {

int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };

int n = arr.length;

radixsort(arr, n);

print(arr, n);

MERGE SORT:
import java.io.*;

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

QUICK SORT:
class QuickSort {

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

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;

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

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[] = {10, 7, 8, 9, 1, 5};

int n = arr.length;

QuickSort ob = new QuickSort();

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

System.out.println("Sorted array");

printArray(arr);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy