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 StringMemory { | |
public static void main(String... args) { | |
String literal = "loper"; | |
String object = new String("loper"); | |
System.out.println(literal == object); | |
System.out.println(literal.equals(object)); | |
} |
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 StringMemoryIntern { | |
public static void main(String... args) { | |
String literal = "loper"; | |
String object = new String("loper"); | |
String intern = object.intern(); | |
System.out.println(literal == object); // false | |
System.out.println(literal.equals(object)); // true | |
System.out.println(literal == intern); // true |
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 BubbleSort { | |
public static void bubbleSort(int[] arr) { | |
final int length = arr.length; | |
for (int i = 0; i < length; i++) { | |
for (int j = 0; j < length - i; j++) { | |
if (j + 1 < length && arr[j] > arr[j + 1]) { | |
arr[j] = arr[j] + arr[j + 1]; | |
arr[j + 1] = arr[j] - arr[j + 1]; | |
arr[j] = arr[j] - arr[j + 1]; |
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 SelectionSort { | |
public static void selectionSort(int[] arr) { | |
final int length = arr.length; | |
for (int i = 0; i < length; i++) { | |
int min = i; | |
for (int j = i + 1; j < length; j++) { | |
if (arr[j] < arr[min]) { | |
min = j; |
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 InsertionSort { | |
public static void insertionSort(int[] arr) { | |
final int length = arr.length; | |
for (int i = 1; i < length; i++) { | |
final int key = arr[i]; | |
int position = i; | |
while (position > 0 && key < arr[position - 1]) { | |
arr[position] = arr[position - 1]; |
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 ShellSort { | |
public static void shellSort(int[] arr) { | |
final int length = arr.length; | |
int interval = length / 2; | |
while (interval >= 1) { | |
for (int i = 0; i < length; i++) { | |
intervalSort(arr, i, length - 1, interval); | |
} |
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 QuickSort { | |
public static void quickSort(int[] arr) { | |
quickSort(arr, 0, arr.length -1); | |
} | |
private static void quickSort(int[] arr, int begin, int end) { | |
if (begin >= end) { | |
return; | |
} |
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 HeapSort { | |
public static void heapSort(int[] arr) { | |
final int length = arr.length; | |
final Heap heap = new Heap(length); | |
for (int element : arr) { | |
heap.insertHeap(element); | |
} | |
for (int i = length - 1; i >= 0; --i) { |
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 MergeSort { | |
private static int[] sorted; | |
public static void mergeSort(int[] arr) { | |
sorted = new int[arr.length]; | |
mergeSort(arr, 0, arr.length - 1); | |
} | |
private static void mergeSort(int[] arr, int begin, int end) { | |
if (begin < end) { |
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 RadixSort { | |
@SuppressWarnings("unchecked") | |
private static LinkedList<Integer>[] counter = new LinkedList[] { | |
new LinkedList<>(), new LinkedList<>(), | |
new LinkedList<>(), new LinkedList<>(), | |
new LinkedList<>(), new LinkedList<>(), | |
new LinkedList<>(), new LinkedList<>(), | |
new LinkedList<>(), new LinkedList<>() }; |
OlderNewer