LAB # 6
Linear and bubble sort in java
OBJECTIVE # 1:
Implant linear and bubble sort in java discuss in class (Don’t bulletin array).
SOURCE CODE:
public class NewClass6_1 {
public static void main(String ar[]){
System.out.println(“linear sort”);
int Data[]={2,4,7,45,44};
int k=1;
for(k=0;k<Data.length;k++){
int ptr=k+1;
while(ptr<Data.length){
if(Data[k]>Data[ptr]){
int a=Data[k];
Data[k]=Data[ptr];
Data[ptr]=a;
}
ptr=ptr+1;
}
}
for(int i=0;i<Data.length;i++){
System.out.print(Data[i]+”,”);
}
System.out.println(“\rbubble sort”);
int Data1[]={22,23,80,10,32};
int k1;
for(k1=1;k1<Data1.length;k1++){
int ptr=0;
while(ptr<Data1.length-k1){
if(Data1[ptr]>=Data1[ptr+1]){
int temp=Data1[ptr];
Data1[ptr]=Data1[ptr+1];
Data1[ptr+1]=temp;
}
ptr=ptr+1;
}
}
for(int i=0;i<Data1.length;i++){
System.out.print(Data1[i]+”,”);
}
}
}
Output:
linear sort
2,4,7,44,45,
bubble sort
10,22,23,32,80,
OBJECTIVE # 2:
Implant array sort for sorting in integer char double. Also sort for specific range.
SOURCE CODE:
import java.util.*;
public class lab6_2 {
public static void main(String ar[]){
int[] i={12,14,15,16,3};
double[] j={0.2,0.3,0.4,0.5,0.6};
char[] r={‘o’,’p’,’r’,’e’,’v’};
Arrays.sort(i);
Arrays.sort(j);
Arrays.sort(r);
System.out.println(“INTGER”);
for(int k=0;k<i.length;k++){
System.out.println(i[k]);
}
System.out.println(“Double”);
for(int k=0;k<j.length;k++){
System.out.println(j[k]);
}
System.out.println(“Double”);
for(int k=0;k<r.length;k++){
System.out.println(r[k]);
}
Arrays.sort(i,0,2);
Arrays.sort(j,2,3);
Arrays.sort(r,1,3);
System.out.println(“Spacific “);
System.out.println(“INTGER”);
for(int k=0;k<2;k++){
System.out.println(i[k]);
}
System.out.println(“Double”);
for(int k=0;k<3;k++){
System.out.println(j[k]);
}
System.out.println(“Double”);
for(int k=0;k<3;k++){
System.out.println(r[k]);
}
}
}
Output:
INTGER
3
12
14
15
16
Double
0.2
0.3
0.4
0.5
0.6
Double
e
o
p
r
v
Spacific
INTGER
3
12
Double
0.2
0.3
0.4
Double
e
o
p
OBJECTIVE # 3:
Readel the algotithm of linear and bubble sort form maximum to minimum string (Don’t use array .sort)
SOURCE CODE:
public class lab6_3 {
public static void main(String ar[]){
System.out.println(“Maxi to Mini”);
int Data[]={22,23,80,10,32};
int k;
for(k=1;k<Data.length;k++){
int ptr=0;
while(ptr<Data.length-k){
if(Data[ptr]<Data[ptr+1]){
int temp=Data[ptr];
Data[ptr]=Data[ptr+1];
Data[ptr+1]=temp;
}
ptr=ptr+1;
}
}
for(int i=0;i<Data.length;i++){
System.out.print(Data[i]+”,”);
}
}
}
Output:
Maxi to Mini
80,32,23,22,10,
OBJECTIVE # 3
Implement lab task 1 and 3 for string(alphabet) .
SOURCE CODE
public class lab6_h {
public static void main(String[] args) {
String[] list = {“umair”, “hamza”, “ahsan”, “junaid”, “suhfa”};
System.out.print(“STRING ARRAY : “);
for(int w = 0; w < list.length; w++){
System.out.print(list[w]+”, “);
}// end for loop
System.out.println(“\n\n——————–“);
System.out.println(“ASCENDING ORDER”);
System.out.println(“——————–“);
linear_Sort_Ascending(list);
System.out.print(“\nLINEAR SORT : “);
for(int b = 0; b < list.length; b++){
System.out.print(list[b]+”, “);
}// end for loop
bubble_Sort_Ascending(list);
System.out.print(“\nBUBBLE SORT : “);
for(int s = 0; s < list.length; s++){
System.out.print(list[s]+”, “);
}// end for loop
System.out.println(“\n\n——————–“);
System.out.println(“DESCENDING ORDER”);
System.out.println(“——————–“);
linear_Sort_Descending(list);
System.out.print(“\nLINEAR SORT : “);
for(int b = 0; b < list.length; b++){
System.out.print(list[b]+”, “);
}// end for loop
bubble_Sort_Descending(list);
System.out.print(“\nBUBBLE SORT : “);
for(int s = 0; s < list.length; s++){
System.out.print(list[s]+”, “);
}// end for loop
}// end main method
public static void linear_Sort_Ascending(String[] l){
for(int k = 0; k < l.length; k++){
int ptr = k + 1;
while(ptr < l.length){
if(l[k].compareToIgnoreCase(l[ptr]) > 0){
String temp = l[k];
l[k] = l[ptr];
l[ptr] = temp;
}// end if
ptr = ptr + 1;
}//end while
}// end for
}// end method
public static void bubble_Sort_Ascending(String[] l){
for(int k = 1; k < l.length; k++){
int ptr = 0;
while(ptr < l.length – k){
if(l[ptr].compareToIgnoreCase(l[ptr + 1]) > 0){
String temp = l[ptr];
l[ptr] = l[ptr + 1];
l[ptr + 1] = temp;
}// end if
ptr = ptr + 1;
}//end while
}// end for
}// end method
public static void linear_Sort_Descending(String[] l){
for(int k = 0; k < l.length; k++){
int ptr = k + 1;
while(ptr < l.length){
if(l[k].compareToIgnoreCase(l[ptr]) < 0){
String temp = l[k];
l[k] = l[ptr];
l[ptr] = temp;
}// end if
ptr = ptr + 1;
}//end while
}// end for
}// end method
public static void bubble_Sort_Descending(String[] l){
for(int k = 1; k < l.length; k++){
int ptr = 0;
while(ptr < l.length – k){
if(l[ptr].compareToIgnoreCase(l[ptr + 1]) < 0){
String temp = l[ptr];
l[ptr] = l[ptr + 1];
l[ptr + 1] = temp;
}// end if
ptr = ptr + 1;
}//end while
}// end for
}// end method
}
Output:
STRING ARRAY : umair, hamza, ahsan, junaid, suhfa,
——————–
ASCENDING ORDER
——————–
LINEAR SORT : ahsan, hamza, junaid, suhfa, umair,
BUBBLE SORT : ahsan, hamza, junaid, suhfa, umair,
——————–
DESCENDING ORDER
——————–
LINEAR SORT : umair, suhfa, junaid, hamza, ahsan,
BUBBLE SORT : umair, suhfa, junaid, hamza, ahsan,