Reputation: 47
I am just trying to know how this code still works while the methods selection, insertion, bubble return void. Is it because the methods are static or what?
Also, how can one swap variables without XOR'ing them?
public class Sorts {
static int[] a = { 100, 90, 70, 60, 50, 40, 30, 20, 10, 7, 6, 5, 4, 3, 2, -1 };
public static void main(String[] args) {
// TODO Auto-generated method stub
bubbleSort(a);// Worst one
print(a);
int[] b = {100, 90, 70, 60, 50, 40, 30, 20, 10, 7, 6, 5, 4, 3, 2, -1};
MY_SEMI_SELECTION_Sort(b);
print(b);
int[] e = { 100, 90, 70, 60, 50, 40, 30, 20, 10, 7, 6, 5, 4, 3, 2, -1};
selectionSort2(e);
print(e);
int[] c = {100, 90, 70, 60, 50, 40, 30, 20, 10, 7, 6, 5, 4, 3, 2, -1};
insertionSort(c); // The best so far
print(c);
}
static void insertionSort(int[] arr) {
int len = arr.length;
for (int i = 1; i < len; ++i) {
int j = i;
while (j > 0 && arr[j] < arr[j - 1]) {
// Swap
arr[j] ^= arr[j - 1] ^= arr[j];
arr[j - 1] ^= arr[j];
--j;
}
}
}
static void MY_SEMI_SELECTION_Sort(int[] arr) {
int len = arr.length;
for (int i = 0; i < len; ++i) {
for (int k = i; k < len - 1; ++k) {
if (arr[i] > arr[k + 1]) {
// Swapping........
arr[i] ^= arr[k + 1] ^= arr[i];
arr[k + 1] ^= arr[i];
}
}
}
}
static void selectionSort2(int[] arr) {
int len = arr.length;
for (int i = 0; i < len; ++i) {
int k;
int minIndex = i;
for (k = i; k < len - 1; ++k) {
if (arr[minIndex] > arr[k + 1]) {
minIndex = k + 1;
}
}
if (arr[minIndex] != arr[i]) {
arr[minIndex] ^= arr[i] ^= arr[minIndex];
arr[i] ^= arr[minIndex];
}
}
}
static void bubbleSort(int[] arr) {
int len = arr.length;
for (int i = 0; i < len; ++i) {
for (int k = 0; k < len - 1; ++k) {
if (arr[k] > arr[k + 1]) {
// Swapping........
arr[k] ^= arr[k + 1] ^= arr[k];
arr[k + 1] ^= arr[k];
}
}
}
}
static void swap(int a, int b) {
int temp = b;
b = a;
a = temp;
}
static void print(int[] arr) {
int len = arr.length;
for (int i = 0; i < len; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Upvotes: 0
Views: 783
Reputation: 11131
Why are my methods working even though they return void?
Java is always pass-by-value. But what value are we talking about? The value being passed to the functions is the reference value, imagine it like the address of where the array is located. So, you're always referring to the same array, both inside and outside your methods.
How to swap elements without Xoring them?
Imagine variables as buckets, you cannot fill one without losing its previous content. You need to use a temporary variable
int a=1,b=2;
int tmp=a;
a=b; //without the previous line of code you'd have lost the value of 'a'
b=tmp;
Upvotes: 2
Reputation: 3260
is it because the methods are STATIC No, because int array a is a reference only. While calling different methods the same reference is passed as parameter. Even result will be the same if int array a is local variable to main.
how to swap elements without Xoring them The best way to swap variables is via XOR.
a = a ^ b;
b = a ^ b;
a = a ^ b;
The other way is via using temp variable.
Upvotes: -1