hsipeng / java_learnning_trip

spring springMVC mybatis
0 stars 1 forks source link

java中常见的几种排序方法 #50

Open hsipeng opened 7 years ago

hsipeng commented 7 years ago
public class BubbleSort { 
public static void sort(int data[]) { 
for (int i = 0; i < data.length -1; i++) { 
for (int j = 0; j < data.length - i - 1; j++) { 
if (data[j] > data[j + 1]) { 
int temp = data[j]; 
data[j] = data[j + 1]; 
data[j + 1] = temp; 
}

        }  
    }  
}  
}
public class SelectionSort { 
public static void sort(int data[]) { 
int minVal; 
int minIndex; 
for (int i = 0; i < data.length - 1; i++) { 
minVal = data[i]; 
minIndex = i; 
for (int j = i + 1; j < data.length; j++) { 
if (data[j] < minVal) { 
minVal = data[j]; 
minIndex = j; 
} 
} 
if (minVal != data[i] && minIndex != i) { 
data[minIndex] = data[i]; 
data[i] = minVal; 
} 
}

}  
}
public class QuickSort { 
public static void sort(int data[], int start, int end) { 
if (end - start <= 0) { 
return; 
} 
int last = start; 
for (int i = start + 1; i <= end; i++) { 
if (data[i] < data[start]) { 
int temp = data[++last]; 
data[last] = data[i]; 
data[i] = temp; 
} 
} 
int temp = data[last]; 
data[last] = data[start]; 
data[start] = temp; 
sort(data, start, last - 1); 
sort(data, last + 1, end); 
}

}