Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Insertion Sort (Java) #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Array Sorting/Insertion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*RockLee444*/
import java.util.Scanner;
public class Insertion{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] data = new int[1]; //Initializing array
int size=0,error=0; //Initializing variables
do{
error=0;
System.out.println("How many numbers do you wish to sort?");
size = sc.nextInt();
if(size>0){
data = new int[size];
for(int i=0;i<size;i++){
System.out.println("Add a number.");
data[i]=sc.nextInt();
}
}
else {
error=1;
}
}while(error!=0);
sc.close();
insertion(data,size);
}

public static void insertion(int[] data,int size){
int aux,j;
for(int i=1;i<size;i++){
j=i;
aux=data[i];
while(j>0 && aux<data[j-1]){
data[j] = data[j-1];
j--;
}
data[j] = aux;
}
visualize(data, size);
}

public static void visualize(int data[],int size){
System.out.println("\t*******Sorted array:*******");

for(int i=0;i<size;i++){
System.out.println("Number in position " + i + ": " + data[i]);
}
}
}