冒泡排序
思路:将数组中两个相邻的数进行比较,如果前一个数大,就将两个数交换位置,这样就能实现将大的数往后移,就像冒泡一样浮到上面。
class Solution {
public int[] sortArray(int[] nums) {
for(int i = 0; i < nums.length - 1; i++){
for(int j = 0; j < nums.length - i - 1; j++){
if(nums[j] > nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
return nums;
}
}
选择排序
思路:先定义一个记录最小元素的下标,然后循环一次后面的元素,找到最小的元素,最后将他放到前面排序好的序列,前面的元素就是从小到大排好序的。
class Solution {
public int[] sortArray(int[] nums) {
int index = 0;
for(int i = 0; i < nums.length;i++){
index = i;
for(int j = i + 1; j < nums.length;j++){
if(nums[index] > nums[j])
index = j;
}
int temp = nums[index];
nums[index] = nums[i];
nums[i] = temp;
}
return nums;
}
}
插入排序
思路:定义一个待插入的数,再定义一个待插入数的前一个数的下标,将待插入的数依次与它前面的元素比较,如果前面的数大,就将前面的数后移一位,最后将待插入的数放到第一个比它小的数后面。
class Solution {
public int[] sortArray(int[] nums) {
for(int i = 0; i < nums.length; i++){
int insertValue = nums[i];
int insertIndex = i - 1;
while(insertIndex >= 0 && nums[insertIndex] > insertValue){
nums[insertIndex + 1] = nums[insertIndex];
insertIndex--;
}
nums[insertIndex + 1] = insertValue;
}
return nums;
}
}
希尔排序
思路:希尔排序是插入排序的升级版,将数组按照一定的间隔进行分组,将每组使用插入排序进行排序。
class Solution {
public int[] sortArray(int[] nums) {
int interval = nums.length / 2;
while(interval >= 1){
for(int i = 0; i < nums.length; i++){
int insertValue = nums[i];
int insertIndex = i - interval;
while(insertIndex >= 0 && nums[insertIndex] > insertValue){
nums[insertIndex + interval] = nums[insertIndex];
insertIndex=insertIndex-interval;
}
nums[insertIndex + interval] = insertValue;
}
interval = interval / 2;
}
return nums;
}
}
快速排序
思路:定义一个基准元素,一般是数组第一个元素,再定义一个低指针指向数组第一个元素和一个高指针指向数组最后一个元素,高指针先往左移,当遇到第一个比基准元素小的数停止,然后低指针往右移遇到第一个比基准元素大的数停止,交换两个指针所指向的元素。之后两个指针继续移动直到两指针相遇,再将基准元素与两指针共同所指向的元素进行交换。此时,该基准元素已经到达了排好序的位置,之后用递归将基准元素前面和后面的元素进行排序。
class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length - 1);
return nums;
}
public static void quickSort(int[] a,int low, int high){
if(low > high)
return;
int i = low;
int j = high;
int temp = a[low];
while(i!=j){
while(a[j] >= temp && i < j){
j--;
}
while(a[i] <= temp && i < j){
i++;
}
if(i < j){
int t = a[j];
a[j] = a[i];
a[i] = t;
}
}
a[low] = a[i];
a[i] = temp;
quickSort(a,low,i - 1);
quickSort(a,i+1,high);
}
}
归并排序
思路:先分再合,先用二分法将数组分到最小,将分完之后的两个数组按照从小到大放到这个临时数组中,最后将临时数组的内容放到原数组中。
class Solution {
public int[] sortArray(int[] nums) {
int[] temp = new int[nums.length];
mergeSort(nums,0,nums.length - 1,temp);
return nums;
}
public static void mergeSort(int[] a,int low,int high,int[] temp){
if(low != high){
int center = (low + high)/2;
mergeSort(a,low,center,temp);
mergeSort(a,center + 1,high,temp);
merge(a,low,high,center,temp);
}
}
public static void merge(int[] a,int low,int high,int center,int[] temp){
int i = low;
int j = center + 1;
int t = 0;
while(i<=center && j <=high){
if(a[i]<a[j]){
temp[t] = a[i];
i++;
t++;
}else{
temp[t] = a[j];
j++;
t++;
}
}
while(i <= center){
temp[t] = a[i];
i++;
t++;
}
while(j <= high){
temp[t] = a[j];
j++;
t++;
}
i = low;
t = 0;
while(i <= high){
a[i] = temp[t];
i++;
t++;
}
}
}