function ArrayList() {
var array = [];
this.swap = function(index1, index2) {
var aux = array[index1];
array[index1] = array[index2];
array[index2] = aux;
}
var swapQuickSort = function(arrayList, index1, index2) {
var aux = arrayList[index1];
arrayList[index1] = arrayList[index2];
arrayList[index2] = aux;
}
this.print = function() {
console.log(array.join())
}
this.insert = function(item) {
array.push(item);
}
this.toString = function() {
return array.join();
}
this.bubbleSort = function() {
var length = array.length;
for (var i = 0; i < length; i++) {
for (var j = 0; j < length - 1; j++) {
if (array[j] > array[j + 1]) {
this.swap(j, j + 1);
}
}
}
}
this.createNonSortedArrary = function(size) {
var array = new ArrayList();
for (var i = size; i > 0; i--) {
array.insert(i);
}
return array;
}
this.adBubbleSort = function() {
var length = array.length;
for (var i = 0; i < length; i++) {
for (var j = 0; j < length - 1; j++) {
if (array[j] > array[j + 1]) {
this.swap(j, j + 1);
}
}
}
}
this.selectedSort = function() {
var length = array.length,
indexMin;
for (var i = 0; i < length - 1; i++) {
indexMin = i;
for (var j = i; j < length; j++) {
if (array[indexMin] > array[j]) {
indexMin = j;
}
}
if (i !== indexMin) {
this.swap(i, indexMin);
}
}
}
this.insertionSort = function() {
var length = array.length,
j,
temp;
for (var i = 0; i < length; i++) {
j = i;
temp = array[i];
while (j > 0 && array[j - 1] > temp) {
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
}
var merge = function(left, right) {
var result = [],
il = 0,
ir = 0;
while (il < left.length && ir < right.length) {
if (left[il] < right[ir]) {
result.push(left[il++]);
} else {
result.push(right[ir++]);
}
}
while (il < left.length) {
result.push(left[il++]);
}
while (ir < right.length) {
result.push(right[ir++]);
}
return result;
}
var mergeSortRec = function(arrayList) {
var length = arrayList.length;
if (length == 1) {
return arrayList;
}
var mid = Math.floor(length / 2),
left = arrayList.slice(0, mid),
right = arrayList.slice(mid, length);
return merge(mergeSortRec(left), mergeSortRec(right));
}
this.mergeSort = function() {
array = mergeSortRec(array);
}
var parition = function(arrayList, left, right) {
var pivot = arrayList[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (arrayList[i] < pivot) {
i++;
}
while (arrayList[j] > pivot) {
j--;
}
if (i <= j) {
swapQuickSort(arrayList, i, j);
i++;
j--;
}
}
return i;
}
var quick = function(arrayList, left, right) {
var index;
if (arrayList.length > 1) {
index = parition(arrayList, left, right);
if (left < index - 1) {
quick(arrayList, left, index - 1);
}
if (index < right) {
quick(arrayList, index, right);
}
}
}
this.quickSort = function() {
quick(array, 0, array.length - 1);
}
this.clear = function() {
array = [];
}
this.sequentialSearch = function(item) {
for (var i = 0; i < array.length; i++) {
if (item == array[i]) {
return i;
}
}
return -1;
}
this.binarySearch = function(item) {
this.quickSort();
var low = 0,
high = array.length - 1,
mid,
element;
while (low <= high) {
mid = Math.floor((low + high) / 2);
element = array[mid];
if (element < item) {
low = mid + 1;
} else if (element > item) {
high = mid - 1
}else{
return mid;
}
}
}
}
var insertDefaultValue = function(array) {
array.insert(10);
array.insert(15);
array.insert(20);
array.insert(0);
array.insert(2);
array.insert(1);
}
var arrayList = new ArrayList();
insertDefaultValue(arrayList);
console.log("普通冒泡")
arrayList.bubbleSort();
arrayList.print();
arrayList.clear();
console.log("高级冒泡")
insertDefaultValue(arrayList);
arrayList.adBubbleSort();
arrayList.print();
arrayList.clear();
console.log("高级冒泡")
insertDefaultValue(arrayList);
arrayList.adBubbleSort();
arrayList.print();
arrayList.clear();
console.log("选择排序")
insertDefaultValue(arrayList);
arrayList.selectedSort();
arrayList.print();
arrayList.clear();
console.log("插入排序")
insertDefaultValue(arrayList);
arrayList.insertionSort();
arrayList.print();
arrayList.clear();
console.log("归并排序")
insertDefaultValue(arrayList);
arrayList.mergeSort();
arrayList.print();
arrayList.clear();
console.log("快速排序")
insertDefaultValue(arrayList);
arrayList.quickSort();
arrayList.print();
console.log(arrayList.sequentialSearch(15));
console.log(arrayList.binarySearch(20));
arrayList.clear();