SlideShare a Scribd company logo
1 of 5
Download to read offline
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

Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfarishmarketing21
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfanujsharmaanuj14
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdfAnkitchhabra28
 
Write the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdfWrite the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdfarihanthtoysandgifts
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfanwarsadath111
 
Code javascript
Code javascriptCode javascript
Code javascriptRay Ray
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdfanupamfootwear
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
Write a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdfWrite a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdfarchanaemporium
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdfapexelectronices01
 

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

Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
 
Write the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdfWrite the program in MIPS that declares an array of positive integer.pdf
Write the program in MIPS that declares an array of positive integer.pdf
 
C programs
C programsC programs
C programs
 
ISCP internal.pdf
ISCP internal.pdfISCP internal.pdf
ISCP internal.pdf
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Huraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docxHuraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docx
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Write a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdfWrite a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdf
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Ada file
Ada fileAda file
Ada file
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 

More from eyewatchsystems

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

More from eyewatchsystems (20)

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

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 

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(" "); } } }