Here is program which allow you to sort an array with complexity of (n square) in ascending order Here is the program: here it uses 2 for loop to compare
Example:- Program
import java.util.Scanner;
public class inertsort {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("enter how much element u want in array ");
i=sc.nextInt();
int intsort[] = new int[i];
System.out.println("enter element s");
for(int a=0;a intsort[a]=sc.nextInt();
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}
for(int m =1;m int temp;
for(int n=m;n>0;n--){
{
if(intsort[n] temp=intsort[n];
intsort[n]=intsort[n-1];
intsort[n-1]=temp;
}
}
}
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}
}
}
Output:
enter how much element u want in array
5
enter element s
100
77
55
85
91
Elements in array are
100 77 55 85 91Elements in array are
55 77 85 91 100
Example:- Program
import java.util.Scanner;
public class inertsort {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("enter how much element u want in array ");
i=sc.nextInt();
int intsort[] = new int[i];
System.out.println("enter element s");
for(int a=0;a intsort[a]=sc.nextInt();
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}
for(int m =1;m
for(int n=m;n>0;n--){
{
if(intsort[n]
intsort[n]=intsort[n-1];
intsort[n-1]=temp;
}
}
}
}
System.out.println("Elements in array are ");
for(int a=0;a System.out.print(" "+intsort[a]);
}
}
}
Output:
enter how much element u want in array
5
enter element s
100
77
55
85
91
Elements in array are
100 77 55 85 91Elements in array are
55 77 85 91 100
No comments:
Post a Comment