Insertion sort
Here's the version that prints out the stages that the program goes through:
This file contains 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
public class Sorting { | |
public static void main(String[] args) { | |
int[] array = { 8, 7, 6, 5, 4, 3, 2, 1 }; | |
for (int i=1; i<array.length; i++) { | |
int j = i; | |
int a = array[i]; | |
/**/ // Printing out the current state of things | |
/**/ for (int x=0; x<array.length; x++) { | |
/**/ if (x == i) { | |
/**/ System.out.print("[" + array[x] + "]" + " "); | |
/**/ } | |
/**/ else { | |
/**/ System.out.print(" " + array[x] + " " + " "); | |
/**/ } | |
/**/ } | |
/**/ System.out.println(); | |
while (j > 0 && array[j-1] > a) { | |
/**/ // Printing out the current state of things | |
/**/ for (int x=0; x<array.length; x++) { | |
/**/ if (x == i) { | |
/**/ System.out.print("[" + array[x] + "]" + " "); | |
/**/ } | |
/**/ else if (x == j) { | |
/**/ System.out.print("(" + array[x] + ")" + " "); | |
/**/ } | |
/**/ else { | |
/**/ System.out.print(" " + array[x] + " " + " "); | |
/**/ } | |
/**/ } | |
/**/ System.out.println(); | |
array[j] = array[j-1]; | |
j--; | |
} | |
array[j] = a; | |
} | |
System.out.println(java.util.Arrays.toString(array)); | |
} | |
} | |
I count the letters in the words...
This file contains 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
public class Stuff { | |
public static void main(String[] args) { | |
String text = "más letras"; | |
int[] counts = new int[26]; | |
for (int z=0; z<26; z++) { | |
for (int q=0; q<text.length(); q++) { | |
char here = text.toLowerCase().charAt(q); | |
char letter = (char)('a' + z); | |
if (here == letter) { | |
counts[z]++; | |
} | |
} | |
} | |
for (int i=0; i<26; i++) { | |
if (counts[i] != 0) { | |
System.out.println((char)('a' + i) + ": " + counts[i]); | |
} | |
} | |
} | |
} | |
This program, sadly, will not handle åccénts or l33t h4><0r sp34k, but I was delighted to see that it *can* handle letters with v⃗ector a⃗rrows over them!
No comments:
Post a Comment