0% found this document useful (0 votes)
6 views5 pages

Ds (Hitesh) PDF

This document contains code for implementing three sorting algorithms: bubble sort, insertion sort, and selection sort. It defines methods for each algorithm to sort an integer array. It also includes a main method to test each algorithm by taking user input for array size and elements, calling the appropriate sort method, and displaying the sorted output.

Uploaded by

alphacrusher21
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)
6 views5 pages

Ds (Hitesh) PDF

This document contains code for implementing three sorting algorithms: bubble sort, insertion sort, and selection sort. It defines methods for each algorithm to sort an integer array. It also includes a main method to test each algorithm by taking user input for array size and elements, calling the appropriate sort method, and displaying the sorted output.

Uploaded by

alphacrusher21
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/ 5

BUBBLE SORT:-

import java.util.Scanner;

public class binary {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan=new Scanner (System.in);

int size=scan.nextInt();

int a[] = new int [size];

for (int i=0; i<a.length;++i) {

a[i]=scan.nextInt();

sorted(a,size);

display(a);

System.out.println("CREATED BY HITESH CHAND KATOCH");

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

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

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

if (arr[j]>arr[j+1]) {

int temp=arr[j+1] ;

arr[j+1]=arr[j];

arr[j]=temp;
}

static void display(int a[]) {

for (int i=0; i<a.length;++i) {

System.out.println(a[i]);

INSERTION SORT :-
import java.util.Scanner;

public class insertionsort {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan=new Scanner (System.in);

int size=scan.nextInt();

int a[] = new int [size];

for (int i=0; i<a.length;++i) {

a[i]=scan.nextInt();

sorted(a,size);

display(a);

System.out.println("CREATED BY HITESH CHAND KATOCH");

/*static void sorted (int n , int a[]) {


int ind;

for (int i=0;i<a.length;++i) {

ind =i;

for (int j=i+1;j<a.length;++j) {

if (a[j]<a[ind]) {

ind =j;

else {

int temp =a[ind];

a[ind]=a[i];

a[i]=temp;

}*/

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

int min;

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

min = i;

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

if (arr[j] < arr[min])

min = j;

int temp = arr[min];

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

static void display(int a[]) {

for (int i=0; i<a.length;++i) {

System.out.println(a[i]);

SELECTION SORT :-
import java.util.Scanner;

public class SELEC {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan=new Scanner (System.in);

int size=scan.nextInt();

int a[] = new int [size];

for (int i=0; i<a.length;++i) {

a[i]=scan.nextInt();

sorted(size,a);

System.out.println("CREATED BY HITESH CHAND KATOCH");

static void sorted (int n , int a[]) {

int i,j,k;

for (i=0;i<a.length;++i) {
k =a[i];

j=i-1;

while (j>=0 && a[j]>k) {

a[j+1]=a[j];

j=j-1;

a[j+1]=k;

display(a);

//System.out.println();

static void display(int a[]) {

for (int i=0; i<a.length;++i) {

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

System.out.println();

You might also like