Created
March 27, 2015 09:45
-
-
Save MarksCode/1c938ab80bd8945c54f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**********************/ | |
/* Integer Sorter */ | |
/* by Ron Marks */ | |
/* */ | |
/* This program sorts */ | |
/* a list of integers */ | |
/* the user types in. */ | |
/**********************/ | |
import java.util.Scanner; | |
class Sort { | |
private static int[] a; | |
public static void main(String[] args) { | |
System.out.println("How many numbers to be sorted?"); | |
Scanner scan = new Scanner(System.in); | |
int sz = scan.nextInt(); | |
a = new int[sz]; | |
System.out.println("Enter integer to be sorted:"); | |
for(int i=0; i<a.length; i++){ | |
a[i] = scan.nextInt(); | |
} | |
while(true){ | |
boolean st = isSorted(); | |
if(st) break; | |
sort(); | |
} | |
System.out.println(); | |
System.out.println("Your Sorted Numbers:"); | |
for(int j=0; j<a.length; j++){ | |
System.out.println(j + " : " + a[j]); | |
} | |
} | |
public static void sort(){ | |
int b = a.length; | |
int c = 0; | |
for(int i=0; i<b-1; i++){ | |
for(int d=i+1; d<b; d++){ | |
c = a[d]; | |
if(a[i] > c){ | |
int temp = a[i]; | |
a[i] = c; | |
a[d] = temp; | |
} | |
} | |
} | |
} | |
public static boolean isSorted(){ | |
for(int i=0; i<a.length-1; i++){ | |
if(a[i] > a[i+1]){ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment