Advertisement
Guest User

Ninja we made it

a guest
Nov 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. public class Sorter {
  3.  
  4. /*public static void selectionSort(int[] arr) {
  5. for (int n=arr.length; n > 1; n--) {
  6. int maxIndex = 0;
  7. for (int i=1; i < n; i++) {
  8. if (arr[i] > arr[maxIndex]) {
  9. maxIndex = i;
  10. }
  11. }
  12. // switch with last
  13. int temp = arr[maxIndex];
  14. arr[maxIndex] = arr[n-1];
  15. arr[n-1] = temp;
  16. }
  17. } */
  18.  
  19. public static void selectionSort(String[] arr) {
  20. for (int n=arr.length; n > 1; n--) {
  21. int maxIndex = 0;
  22. for (int i=1; i < n; i++) {
  23. if (arr[i].compareTo(arr[maxIndex]) > 0) {
  24. maxIndex = i;
  25. }
  26. }
  27. // switch with last
  28. String temp = arr[maxIndex];
  29. arr[maxIndex] = arr[n-1];
  30. arr[n-1] = temp;
  31. }
  32. }
  33.  
  34. public static void printArray(int[] list) {
  35. System.out.print("[");
  36. System.out.print(list[0]);
  37. for (int i=1; i < list.length; i++) {
  38. System.out.print("," + list[i]);
  39. }
  40. System.out.println("]");
  41. }
  42.  
  43. public static void printArray(String[] list) {
  44. System.out.print("[");
  45. System.out.print(list[0]);
  46. for (int i=1; i < list.length; i++) {
  47. System.out.print("," + list[i]);
  48. }
  49. System.out.println("]");
  50. }
  51.  
  52. public static void main(String[] args) {
  53. System.out.println("Enter list of integers to be sorted, comma separated: ");
  54. String line = IO.readString();
  55. String[] items = line.split(","); //saves characters into an array by typing them seperated by commas
  56. String[] arr = new String[items.length];
  57. for (int i=0; i < arr.length; i++) {
  58. arr[i] = (items[i]);
  59. }
  60. System.out.print("Input array :");
  61. printArray(arr);
  62. selectionSort(arr);
  63.  
  64. System.out.print("Sorted array:");
  65. printArray(arr);
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement