js冒泡排序
1 /* 2 值交换函数swap() 3 */ 4 var array = [1,2]; 5 function swap(arr,a,b) 6 { 7 var temp = arr[a]; 8 arr[a] = arr[b]; 9 arr[b] = temp; 10 } 11 swap(array,0,1); 12 document.write(array[0]+"<br>"); 13 document.write(array[1]+"<br>"); 14 15 //3个数的冒泡排序 每次拿第一个数和后面的数比较 16 function sort3(){ 17 var a = 3, b = 9, c = 8, temp; 18 document.write("交换前 a = "+a+" b = "+b+" c = "+c+"<br>"); 19 if(a > b){ 20 temp = a; 21 a = b; 22 b = temp; 23 } 24 if(a > c){ 25 temp = a; 26 a = c; 27 c = temp; 28 } 29 if (b > c){ 30 temp = b; 31 b = c; 32 c = temp; 33 } 34 document.write("交换后 a = "+a+" b = "+b+" c = "+c); 35 } 36 sort3(); 37 38 //冒泡排序每次循环会得出一个最大值沉到最后 39 var array = [1,5,7,9,11,8,3,10]; 40 41 function bubbleSort1(arr){ 42 var i = arr.length,j; 43 var temp; 44 while(i > 0){ 45 for(j = 0; j < i - 1; j++){ 46 if(arr[j] > arr[j+1]){ 47 temp = arr[j]; 48 arr[j] = arr[j+1]; 49 arr[j+1] = temp; 50 } 51 } 52 i--; 53 } 54 return arr; 55 } 56 57 document.write("<br> array = [1,5,7,9,11,8,3,10] 排序后"+bubbleSort1(array)+"<br>"); 58 59 //翻转数组 60 function reverseArray(oldArray){ 61 var newArray = []; 62 for(var i = oldArray.length - 1; i >= 0; i--){ 63 newArray[newArray.length] = oldArray[i]; 64 } 65 return newArray; 66 } 67 var oldArray = [2, 5, 9, 1, 4, 0]; 68 document.write("oldArray = [2, 5, 9, 1, 4, 0]<br> newArray = "+reverseArray(oldArray )); 69 70 71 //已经排好序的数组若想实现倒序反转可以强制两两交换 72 array1 = [1,5,6,9,11,8,3,10] 73 function bubbleSort2(arr){ 74 document.write("<br>"); 75 document.write("未排序的数组 "+arr); 76 var i = arr.length,j; 77 var temp; 78 while(i > 0){ 79 for(j = 0; j < i - 1; j++){ 80 if(arr[j]>arr[j+1]){ 81 swap(arr,j,j+1); 82 } 83 } 84 i--; 85 } 86 document.write("<br>"); 87 document.write("翻转前的数组 "+arr); 88 for(var i = 0; i <= arr.length - 1; i++){ 89 for(var j = 0; j < arr.length - 1- i; j++){ 90 swap(arr,j,j+1);//相邻数据互换位置 91 } 92 } 93 document.write("<br>"); 94 document.write("反转后的数组 "+arr); 95 document.write("<br>"); 96 return arr; 97 } 98 document.write("array1 = [1,5,6,9,11,8,3,10] 排序后"+bubbleSort2(array1)+"<br>"); 99 100 101 arr = [3, 4, 7, 9, 2, 80, 20, 8]; 102 function bubbleSort3(arr){ 103 var q1 = 0; 104 var q2 = 0; 105 for(var i = 0; i < arr.length ; i++){ 106 for(var j = 0; j < arr.length; j++){ 107 var temp; 108 if(arr[j] > arr[j+1]){ 109 temp = arr[j]; 110 arr[j] = arr[j+1]; 111 arr[j+1] = temp; 112 } 113 q1++; 114 } 115 q2++; 116 } 117 document.write("内层循环次数:"+q1+"<br>"); 118 document.write("外层循环次数:"+q2+"<br>"); 119 return arr; 120 } 121 document.write("arr = [3, 4, 7, 9, 2, 80, 20, 8] ="+bubbleSort3(arr)); 122 123 124 arr4 = [3, 4, 7, 9, 2, 80, 20, 8]; 125 function bubbleSort4(arr){ 126 var q1 = 0; 127 var q2 = 0; 128 for(var i = 0; i < arr.length ; i++){ 129 for(var j = 0; j < arr.length - i; j++){ 130 var temp; 131 if(arr[j] > arr[j+1]){ 132 temp = arr[j]; 133 arr[j] = arr[j+1]; 134 arr[j+1] = temp; 135 } 136 q1++; 137 } 138 q2++; 139 } 140 document.write("<br>内层循环次数:"+q1+"<br>"); 141 document.write("外层循环次数:"+q2+"<br>"); 142 return arr; 143 } 144 document.write("arr4 = [3, 4, 7, 9, 2, 80, 20, 8] ="+bubbleSort4(arr4)); 145 146 147 148 arr5 = [3, 4, 7, 9, 2, 80, 20, 8]; 149 function bubbleSort5(arr){ 150 var q1 = 0; 151 var q2 = 0; 152 for(var i = 0; i < arr.length ; i++){ 153 for(var j = 0; j < arr.length - i -1; j++){ 154 var temp; 155 if(arr[j] > arr[j+1]){ 156 temp = arr[j]; 157 arr[j] = arr[j+1]; 158 arr[j+1] = temp; 159 } 160 q1++; 161 } 162 q2++; 163 } 164 document.write("<br>内层循环次数:"+q1+"<br>"); 165 document.write("外层循环次数:"+q2+"<br>"); 166 return arr; 167 } 168 document.write("arr5 = [3, 4, 7, 9, 2, 80, 20, 8] ="+bubbleSort5(arr5)); 169 170 arr6 = [3, 4, 7, 9, 2, 80, 20, 8]; 171 function bubbleSort6(arr){ 172 var q1 = 0; 173 var q2 = 0; 174 for(var i = 0; i < arr.length ; i++){ 175 var isSort = true; 176 for(var j = 0; j < arr.length - i - 1; j++){ 177 var temp; 178 if(arr[j] > arr[j+1]){ 179 isSort = false; 180 temp = arr[j]; 181 arr[j] = arr[j+1]; 182 arr[j+1] = temp; 183 } 184 q1++; 185 } 186 q2++; 187 if(isSort == true){ 188 break; 189 } 190 } 191 document.write("<br>内层循环次数:"+q1+"<br>"); 192 document.write("外层循环次数:"+q2+"<br>"); 193 return arr; 194 } 195 document.write("arr6 = [3, 4, 7, 9, 2, 80, 20, 8] ="+bubbleSort6(arr6)); 196 197 198 //求一组数组中的数的最大值最小值及他们的索引 199 var array = [1, 2, 3, 14, 5, 6, 17, 8, 9, 10]; 200 var max = array[0]; 201 var maxIndex = 0; 202 var min = array[0]; 203 var minIndex = 0; 204 for (var i =0; i < array.length; i++){ 205 if(max < array[i]){ 206 max = array[i]; 207 maxIndex = i; 208 } 209 if(min > array[i]){ 210 min = array[i]; 211 minIndex = i; 212 } 213 } 214 215 console.log("数组中的最大值是 "+max+" 最大值的索引是 "+maxIndex); 216 console.log("数组中的最小值是 "+min+" 最小值的索引是 "+minIndex);