數組 冒泡排序 選擇排序 插入排序
package arr;
/**
* 我自己的數組類
* @author guosheng
*
*/
public class ArraySelf {
public long[] guosheng;
public int elements;
public ArraySelf(){
guosheng = new long[100001];
}
public ArraySelf(int chang){
guosheng = new long[chang];
}
/**
* 添加方法
* 郭勝
*/
public void insert(long value){
guosheng[elements] = value;
elements++;
}
/**
* 降序插入
*/
public void insertBySortAsc(long value){
int i;
for (i = 0; i < elements; i++) {
if(guosheng[i]>value){
break;
}
}
for(int j = elements;j>=i;j--){
guosheng[j+1] = guosheng[j];
}
guosheng[i] = value;
elements ++;
}
/**
* 修改方法
* 根據下標修改
*/
public void change(int index,long value){
if(index > elements || index < 0){
throw new IndexOutOfBoundsException();
}else{
guosheng[index] = value;
}
}
/**
* 根據下標查詢方法
*/
public long searchByIndex(int index){
if(index > elements || index < 0){
throw new IndexOutOfBoundsException();
}else{
return guosheng[index];
}
}
/**
* 根據值線性查詢方法
*/
public int searchByValue(long value){
long startTime = System.currentTimeMillis();
int i;
for(i=0;i<elements;i++){
if(guosheng[i] == value){
break;
}
}
if(i>= elements){
return -1;
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime+"直線執行查詢方法");
return i;
}
/**
* 根據值二分法查詢
*/
public int searchByValueForTwo(long value){
long startTime = System.currentTimeMillis();
int min = 0;
int max = elements;
int middle = (max+min)/2;
for(int i=0;i<elements/2;i++){
if(guosheng[middle] == value){
break;
}else if(i>=(elements/2-1)){
middle = -1;
break;
}else if(guosheng[middle]>value){//向左
max = middle;
middle = (max+min)/2;
}else if(guosheng[middle]<value){//向右
min = middle;
middle = (max + min)/2;
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime+"二分法執行時間");
return middle;
}
/**
* 展示我的數組
*/
public void displayMyArray(){
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < elements; i++) {
sb.append(guosheng[i] + ",");
}
String str = sb.toString();
str = str.substring(0, str.length()-1)+"]";
System.out.println(str);
}
/**
* 冒泡排序
* 郭勝
* @param arr
* @return
*/
public long[] bubblingSort(long[] arr){
long temp = 0;
for(int i=0;i<arr.length-1;i++){
for(int j=arr.length-1;j>i;j--){
if(arr[j]<arr[j-1]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
/**
* 選擇排序
* 郭勝
* @param arr
* @return
*/
public long[] selectionSort(long[] arr){
int k = 0;
long temp = 0;
for(int i=0;i<arr.length-1;i++){
k=i;
for(int j=0;j<arr.length-1;j++){
if(arr[k]<arr[j]){
temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
/**
* 插入排序
* 郭勝
* @param arr
* @return
*/
public long[] insertionSort(long[] arr){
for(int i=1;i<arr.length-1;i++){
if(arr[i]< arr[i-1]){
long temp = arr[i];
int j = i;
while(j>0 && arr[j-1]>temp){
arr[j] = arr[j-1];
j--;
}
arr[j] = temp;
}
}
return arr;
}
}
/**
* 我自己的數組類
* @author guosheng
*
*/
public class ArraySelf {
public long[] guosheng;
public int elements;
public ArraySelf(){
guosheng = new long[100001];
}
public ArraySelf(int chang){
guosheng = new long[chang];
}
/**
* 添加方法
* 郭勝
*/
public void insert(long value){
guosheng[elements] = value;
elements++;
}
/**
* 降序插入
*/
public void insertBySortAsc(long value){
int i;
for (i = 0; i < elements; i++) {
if(guosheng[i]>value){
break;
}
}
for(int j = elements;j>=i;j--){
guosheng[j+1] = guosheng[j];
}
guosheng[i] = value;
elements ++;
}
/**
* 修改方法
* 根據下標修改
*/
public void change(int index,long value){
if(index > elements || index < 0){
throw new IndexOutOfBoundsException();
}else{
guosheng[index] = value;
}
}
/**
* 根據下標查詢方法
*/
public long searchByIndex(int index){
if(index > elements || index < 0){
throw new IndexOutOfBoundsException();
}else{
return guosheng[index];
}
}
/**
* 根據值線性查詢方法
*/
public int searchByValue(long value){
long startTime = System.currentTimeMillis();
int i;
for(i=0;i<elements;i++){
if(guosheng[i] == value){
break;
}
}
if(i>= elements){
return -1;
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime+"直線執行查詢方法");
return i;
}
/**
* 根據值二分法查詢
*/
public int searchByValueForTwo(long value){
long startTime = System.currentTimeMillis();
int min = 0;
int max = elements;
int middle = (max+min)/2;
for(int i=0;i<elements/2;i++){
if(guosheng[middle] == value){
break;
}else if(i>=(elements/2-1)){
middle = -1;
break;
}else if(guosheng[middle]>value){//向左
max = middle;
middle = (max+min)/2;
}else if(guosheng[middle]<value){//向右
min = middle;
middle = (max + min)/2;
}
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime+"二分法執行時間");
return middle;
}
/**
* 展示我的數組
*/
public void displayMyArray(){
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < elements; i++) {
sb.append(guosheng[i] + ",");
}
String str = sb.toString();
str = str.substring(0, str.length()-1)+"]";
System.out.println(str);
}
/**
* 冒泡排序
* 郭勝
* @param arr
* @return
*/
public long[] bubblingSort(long[] arr){
long temp = 0;
for(int i=0;i<arr.length-1;i++){
for(int j=arr.length-1;j>i;j--){
if(arr[j]<arr[j-1]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
/**
* 選擇排序
* 郭勝
* @param arr
* @return
*/
public long[] selectionSort(long[] arr){
int k = 0;
long temp = 0;
for(int i=0;i<arr.length-1;i++){
k=i;
for(int j=0;j<arr.length-1;j++){
if(arr[k]<arr[j]){
temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
/**
* 插入排序
* 郭勝
* @param arr
* @return
*/
public long[] insertionSort(long[] arr){
for(int i=1;i<arr.length-1;i++){
if(arr[i]< arr[i-1]){
long temp = arr[i];
int j = i;
while(j>0 && arr[j-1]>temp){
arr[j] = arr[j-1];
j--;
}
arr[j] = temp;
}
}
return arr;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.