In Java:
Create a bubble, insertion, selection sort algorithm that sorts the following: 6 9 8 12 3 1 7
#source code:
import java.util.*;
public class Test{
public static void bbs(int a[],int bbsl){
for(int i = 0; i < bbsl-1; i++){
for (int j = 0; j < bbsl-i-1; j++){
if(a[j]>a[j+1]){
int val=a[j];
a[j]=a[j+1];
a[j+1] = val;
}
}
}
}
public static void ins(int a[],int insl){
int out,i,j;
for (i=1;i<insl;i++){
out = a[i];
j = i - 1;
while (j >= 0 && a[j] > out){
a[j+1] = a[j];
j = j - 1;
}
a[j+1] = out;
}
}
public static void sss(int a[],int sssl){
int i,j;
for(i = 0; i < sssl-1; i++){
int mini = i;
for (j = i+1; j < sssl; j++)
if (a[j] < a[mini])
mini = j;
int out = a[mini];
a[mini] = a[i];
a[i] = out;
}
}
public static void sortarraysprint(int a[],int ttsl){
for(int i=0;i<ttsl;i++){
System.out.print(a[i]+" ");
}System.out.println("");
}
public static void main(String args[]){
int[] bs1={6,9,8,12,3,1,7};
int[] ss1={6,9,8,12,3,1,7};
int[] is1={6,9,8,12,3,1,7};
bbs(bs1,7);
sortarraysprint(bs1,7);
ins(ss1,7);
sortarraysprint(ss1,7);
sss(is1,7);
sortarraysprint(is1,7);
}
}
#output:
Get Answers For Free
Most questions answered within 1 hours.