package dataSort.select;
/**
* @author zhouushe
*
*/
public class ArraySel {
private long[] a;
private int nElems;
public ArraySel(int max) {
a = new long[max];
this.nElems = 0;
}
public void insert(long value) {
this.a[this.nElems] = value;
this.nElems++;
}
public void display() {
for (int j = 0; j < this.nElems; j++) {
if (j > 0 && j % 10 == 0) {
System.out.println();
}
System.out.print(a[j] + " ");
}
System.out.println();
}
/*
* Compare count: (length - 1) + (length - 2) + ...+ 1 = length * (length -
* 1) / 2
*
* Swap count: (length - 1) / 2 Note: Probability which random data swap is
* one half.
*
* when length ¡ú +¡Þ, Compare count: length * length.
*
* when length ¡ú +¡Þ, Swap count: length.
*
*/
public void selectionSort() {
int out, in, min;
for (out = 0; out < this.nElems - 1; out++) {
min = out;
for (in = out + 1; in < this.nElems; in++) {
if (a[in] < a[min]) {
min = in;
}
}
this.swap(out, min);
}
}
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public static void main(String[] args) {
int maxSize = 100;
ArraySel arr;
arr = new ArraySel(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
System.out.println("*************Before Selection Sort*************");
arr.display();
arr.selectionSort();
System.out.println("*************After Selection Sort*************");
arr.display();
}
}
/**
* @author zhouushe
*
*/
public class ArraySel {
private long[] a;
private int nElems;
public ArraySel(int max) {
a = new long[max];
this.nElems = 0;
}
public void insert(long value) {
this.a[this.nElems] = value;
this.nElems++;
}
public void display() {
for (int j = 0; j < this.nElems; j++) {
if (j > 0 && j % 10 == 0) {
System.out.println();
}
System.out.print(a[j] + " ");
}
System.out.println();
}
/*
* Compare count: (length - 1) + (length - 2) + ...+ 1 = length * (length -
* 1) / 2
*
* Swap count: (length - 1) / 2 Note: Probability which random data swap is
* one half.
*
* when length ¡ú +¡Þ, Compare count: length * length.
*
* when length ¡ú +¡Þ, Swap count: length.
*
*/
public void selectionSort() {
int out, in, min;
for (out = 0; out < this.nElems - 1; out++) {
min = out;
for (in = out + 1; in < this.nElems; in++) {
if (a[in] < a[min]) {
min = in;
}
}
this.swap(out, min);
}
}
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public static void main(String[] args) {
int maxSize = 100;
ArraySel arr;
arr = new ArraySel(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
System.out.println("*************Before Selection Sort*************");
arr.display();
arr.selectionSort();
System.out.println("*************After Selection Sort*************");
arr.display();
}
}