SlideShare a Scribd company logo
Java Assignment
Write a program using sorting
sorting: bubble,selection,insertion,quick,merge
Solution
Bubble Sort:
import java.util.Scanner;
class SortBubble {
public static void main(String []args) {
int number, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of elements to be sorted");
number = in.nextInt();
int ar[] = new int[number];
System.out.println("Enter " + number + " integers");
for (int i = 0; i < number; i++)
ar[i] = in.nextInt();
for (int i = 0; i < ( number - 1 ); i++) {
for (int j = 0; j < number - c - 1; j++) {
if (ar[j] > ar[j+1])
{
swap = ar[j]];
ar[j] = ar[j+1];
ar[j+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (int c = 0; c < number; c++)
System.out.println(ar[c]);
}
}
b) Selection Sort:
public class SortSelection {
public static int[] sort(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;
int temp= arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = sort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
c) Insertion
public class SortInsertion {
public static void main(String[] args) {
int[] array= { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
sort(array);
}
static void printNumbers(int[] input) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ", ");
}
System.out.println(" ");
}
public static void sort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
}
d) Quick Sort:
public class SortQuick {
int array[];
int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
MyQuickSort sorter = new MyQuickSort();
int[] input = {24,2,45,20,56,75,2,56,99,53,12};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}

More Related Content

Similar to Java AssignmentWrite a program using sortingsorting bubble,sele.pdf (20)

PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PDF
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
aloeplusint
 
PPT
16-sorting.ppt
18Gunaalanpg
 
PPT
14-sorting (3).ppt
yasser3omr
 
PPT
14-sorting.ppt
RenalthaPujaBagaskar
 
PPT
14-sorting.ppt
SushantRaj25
 
PPT
14-sorting.ppt
KamalAlbashiri
 
PDF
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
DOC
Bubble sort
Simanjuntak Simanjuntak
 
PPT
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
PDF
Lecture12,13,14.pdf
zainab278016
 
PPT
Unit 7 sorting
kalyanineve
 
PDF
Bin Sorting And Bubble Sort By Luisito G. Trinidad
LUISITO TRINIDAD
 
PDF
Merge Sort java with a few details, please include comments if possi.pdf
feelinggifts
 
PDF
21 Elementary Sorts pdf sorting technique
jiraf23341
 
PDF
21 elementarysorts 2
Hoang Nguyen
 
PPTX
Sortingalgorithminjavamergesortandr.pptx
codevincent624
 
PDF
Ds sorting
Hamza Khan
 
PDF
Selection sort
Abdelrahman Saleh
 
PPTX
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
aloeplusint
 
16-sorting.ppt
18Gunaalanpg
 
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
KamalAlbashiri
 
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Anwar Patel
 
Lecture12,13,14.pdf
zainab278016
 
Unit 7 sorting
kalyanineve
 
Bin Sorting And Bubble Sort By Luisito G. Trinidad
LUISITO TRINIDAD
 
Merge Sort java with a few details, please include comments if possi.pdf
feelinggifts
 
21 Elementary Sorts pdf sorting technique
jiraf23341
 
21 elementarysorts 2
Hoang Nguyen
 
Sortingalgorithminjavamergesortandr.pptx
codevincent624
 
Ds sorting
Hamza Khan
 
Selection sort
Abdelrahman Saleh
 
Different Sorting tecniques in Data Structure
Tushar Gonawala
 

More from eyewatchsystems (20)

PDF
In 9. 3-1 BALANCE SHEET The assets of Dallas & Associates consist e.pdf
eyewatchsystems
 
PDF
I need help understanding the Pan Tompkins algorythm, and Id also .pdf
eyewatchsystems
 
PDF
I believe my chosen answer is correct, but Im not entirely positiv.pdf
eyewatchsystems
 
PDF
how does genies transcript illustrate the statement we learn to .pdf
eyewatchsystems
 
PDF
How many different alleles can a gene have Explain.SolutionEa.pdf
eyewatchsystems
 
PDF
Comment on the behavior of the different types of flexures (crab-leg.pdf
eyewatchsystems
 
PDF
human color-blindness mutations result from A.reciprocal translocat.pdf
eyewatchsystems
 
PDF
Generation 1 2 3 4 6 5 6 7 SolutionThe chances of their first c.pdf
eyewatchsystems
 
PDF
Explain how cell division occurs in the germ (meiosis) cells of anim.pdf
eyewatchsystems
 
PDF
Ecologists are sampling mice from different parks in New York City. M.pdf
eyewatchsystems
 
PDF
Describe the electronic configuration of a given element. Why are we.pdf
eyewatchsystems
 
PDF
Assets are listed on the balance sheet in order ofincreasing size .pdf
eyewatchsystems
 
PDF
1) Even the simplest types of infectious agents must have DNA, RNA, .pdf
eyewatchsystems
 
PDF
1-A) What types of data document patterns and processes of evoluti.pdf
eyewatchsystems
 
PDF
Which of the following does not take place in a signaltransduction pa.pdf
eyewatchsystems
 
PDF
Which term indicates the evolution of many closely related species fr.pdf
eyewatchsystems
 
PDF
you inoculate an LB tube with an aerobic bacterium and incubate it f.pdf
eyewatchsystems
 
PDF
What is meant by the relationship or the two strands of DNA The rig.pdf
eyewatchsystems
 
PDF
Which of the following categories of control can include the logical.pdf
eyewatchsystems
 
PDF
What is it, and how does Chromatic Dispersion Compensation work.pdf
eyewatchsystems
 
In 9. 3-1 BALANCE SHEET The assets of Dallas & Associates consist e.pdf
eyewatchsystems
 
I need help understanding the Pan Tompkins algorythm, and Id also .pdf
eyewatchsystems
 
I believe my chosen answer is correct, but Im not entirely positiv.pdf
eyewatchsystems
 
how does genies transcript illustrate the statement we learn to .pdf
eyewatchsystems
 
How many different alleles can a gene have Explain.SolutionEa.pdf
eyewatchsystems
 
Comment on the behavior of the different types of flexures (crab-leg.pdf
eyewatchsystems
 
human color-blindness mutations result from A.reciprocal translocat.pdf
eyewatchsystems
 
Generation 1 2 3 4 6 5 6 7 SolutionThe chances of their first c.pdf
eyewatchsystems
 
Explain how cell division occurs in the germ (meiosis) cells of anim.pdf
eyewatchsystems
 
Ecologists are sampling mice from different parks in New York City. M.pdf
eyewatchsystems
 
Describe the electronic configuration of a given element. Why are we.pdf
eyewatchsystems
 
Assets are listed on the balance sheet in order ofincreasing size .pdf
eyewatchsystems
 
1) Even the simplest types of infectious agents must have DNA, RNA, .pdf
eyewatchsystems
 
1-A) What types of data document patterns and processes of evoluti.pdf
eyewatchsystems
 
Which of the following does not take place in a signaltransduction pa.pdf
eyewatchsystems
 
Which term indicates the evolution of many closely related species fr.pdf
eyewatchsystems
 
you inoculate an LB tube with an aerobic bacterium and incubate it f.pdf
eyewatchsystems
 
What is meant by the relationship or the two strands of DNA The rig.pdf
eyewatchsystems
 
Which of the following categories of control can include the logical.pdf
eyewatchsystems
 
What is it, and how does Chromatic Dispersion Compensation work.pdf
eyewatchsystems
 
Ad

Recently uploaded (20)

PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
community health nursing question paper 2.pdf
Prince kumar
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Ad

Java AssignmentWrite a program using sortingsorting bubble,sele.pdf

  • 1. Java Assignment Write a program using sorting sorting: bubble,selection,insertion,quick,merge Solution Bubble Sort: import java.util.Scanner; class SortBubble { public static void main(String []args) { int number, c, d, swap; Scanner in = new Scanner(System.in); System.out.println("Please enter the number of elements to be sorted"); number = in.nextInt(); int ar[] = new int[number]; System.out.println("Enter " + number + " integers"); for (int i = 0; i < number; i++) ar[i] = in.nextInt(); for (int i = 0; i < ( number - 1 ); i++) { for (int j = 0; j < number - c - 1; j++) { if (ar[j] > ar[j+1]) { swap = ar[j]]; ar[j] = ar[j+1]; ar[j+1] = swap; } } }
  • 2. System.out.println("Sorted list of numbers"); for (int c = 0; c < number; c++) System.out.println(ar[c]); } } b) Selection Sort: public class SortSelection { public static int[] sort(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; int temp= arr[index]; arr[index] = arr[i]; arr[i] = temp; } return arr; } public static void main(String a[]){ int[] arr1 = {10,34,2,56,7,67,88,42}; int[] arr2 = sort(arr1); for(int i:arr2){ System.out.print(i); System.out.print(", "); } } } c) Insertion
  • 3. public class SortInsertion { public static void main(String[] args) { int[] array= { 4, 2, 9, 6, 23, 12, 34, 0, 1 }; sort(array); } static void printNumbers(int[] input) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + ", "); } System.out.println(" "); } public static void sort(int array[]) { int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] > key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; printNumbers(array); } } } d) Quick Sort: public class SortQuick { int array[];
  • 4. int length; public void sort(int[] inputArr) { if (inputArr == null || inputArr.length == 0) { return; } this.array = inputArr; length = inputArr.length; quickSort(0, length - 1); } private void quickSort(int lowerIndex, int higherIndex) { int i = lowerIndex; int j = higherIndex; int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2]; while (i <= j) { while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { exchangeNumbers(i, j); i++; j--; } } if (lowerIndex < j) quickSort(lowerIndex, j);
  • 5. if (i < higherIndex) quickSort(i, higherIndex); } private void exchangeNumbers(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String a[]){ MyQuickSort sorter = new MyQuickSort(); int[] input = {24,2,45,20,56,75,2,56,99,53,12}; sorter.sort(input); for(int i:input){ System.out.print(i); System.out.print(" "); } } }