您的位置:首页 > 理论基础 > 数据结构算法

java--数据结构--8种排序算法

2016-08-23 09:34 411 查看

1.直接插入排序

直接插入排序:
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
insertionSort(a);
for(int i = 0;i < a.length;i++)
System.out.print(a[i] + " ");
}
// insertionSort必须为static方法,因为不能再静态上下文中引用非静态的方法
public static int[] insertionSort(int a[]) {
for(int i = 1;i < a.length;i++) {
for(int j = i;j > 0;j--) {
if(a[j] < a[j-1]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
return a;
}
}


2.折半插入排序

折半插入排序:
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
binaryInsertSort(a);
for(int i = 0;i< a.length;i++){
System.out.println(a[i] + "");
}
}

public static int[] binaryInsertSort(int a[]) {
for(int i = 1;i < a.length;i++) {
int temp = a[i];
int low = 0;
int high = i-1;

while(low <= high) {
int mid = (low + high)/2;
if(temp < a[mid]) {
high = mid - 1;
}else{
low = mid + 1;
}
}

for(int j = i; j >=low + 1;j--){
a[j] = a[j-1];
}
a[low] = temp;
}
return a;
}
}


3.希尔排序

希尔排序
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
shellInsertSort(a);
for(int i = 0;i< a.length;i++){
System.out.println(a[i] + "");
}
}

public static int[] shellInsertSort(int a[]) {
int d = a.length;
while(true)
{
d = d/2;
for(int x = 0;x < d;x++){
for(int i = x + d;i < a.length;i = i + d) {
int temp = a[i];
int j;
// 注意判断条件j >= 0要在a[j] > temp前边,否则若j为负,a[j] > temp中数组不合法会运行报错
for(j = i - d; j >= 0 && a[j] > temp;j = j-d) {
a[j+d] = a[j];
}
a[j+d] = temp;
}
}
if(d == 1){
break;
}
}
return a;
}
}


4.简单选择排序

简单选择排序
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
selectSort(a);
for(int i = 0;i< a.length;i++){
System.out.println(a[i] + "");
}
}

public static int[] selectSort(int a[]) {
int i,j,minIndex;
for(i = 0;i < a.length;i++){
minIndex = i;
for(j = i + 1;j < a.length;j++){
if(a[minIndex] > a[j]){
minIndex = j;
}
}
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
return a;
}
}


5.堆排序

堆排序:(答案还有问题)
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
heapSort(a);
for(int i = 0;i< a.length;i++){
System.out.print(a[i] + " ");
}
}

public static int[] heapSort(int a[]) {

for(int i = a.length/2 - 1;i >= 0;--i){
heapAdjust(a,i);
}

for(int i = a.length - 1;i > 0;--i) {
int temp = a[i];
a[i] = a[0];
a[0] = temp;
heapAdjust(a,i);
}
return a;
}

public static int[] heapAdjust(int a[],int i) {
int temp = a[i];
int child = 2*i + 1;
while(child < a.length){

if(child + 1 < a.length && a[child] < a[child+1]) {
++child;
}
if(a[i] < a[child]) {
a[i] = a[child];
i = child;
child = 2 * i + 1;
}else {
break;
}
}
a[i] = temp;
return a;
}
}

public class HeapSortTest {

public static void main(String[] args) {
int[] data5 = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
print(data5);
heapSort(data5);
System.out.println("排序后的数组:");
print(data5);
}

public static void swap(int[] data, int i, int j) {
if (i == j) {
return;
}
data[i] = data[i] + data[j];
data[j] = data[i] - data[j];
data[i] = data[i] - data[j];
}

public static void heapSort(int[] data) {
for (int i = 0; i < data.length; i++) {
createMaxdHeap(data, data.length - 1 - i);
swap(data, 0, data.length - 1 - i);
print(data);
}
}

public static void createMaxdHeap(int[] data, int lastIndex) {
for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
// 保存当前正在判断的节点
int k = i;
// 若当前节点的子节点存在
while (2 * k + 1 <= lastIndex) {
// biggerIndex总是记录较大节点的值,先赋值为当前判断节点的左子节点
int biggerIndex = 2 * k + 1;
if (biggerIndex < lastIndex) {
// 若右子节点存在,否则此时biggerIndex应该等于 lastIndex
if (data[biggerIndex] < data[biggerIndex + 1]) {
// 若右子节点值比左子节点值大,则biggerIndex记录的是右子节点的值
biggerIndex++;
}
}
if (data[k] < data[biggerIndex]) {
// 若当前节点值比子节点最大值小,则交换2者得值,交换后将biggerIndex值赋值给k
swap(data, k, biggerIndex);
k = biggerIndex;
} else {
break;
}
}
}
}

public static void print(int[] data) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + "\t");
}
System.out.println();
}

}


6.冒泡排序

冒泡排序:
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
bubbleSort(a);
for(int i = 0;i< a.length;i++){
System.out.print(a[i] + " ");
}
}

public static int[] bubbleSort(int a[]) {
int i = a.length;
int temp,j;
while(i > 0) {
for(j = 0;j < i-1;j++) {
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
i--;
}
return a;
}
}


7.快速排序

快速排序:
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
quickSort(a,0,a.length-1);
for(int i = 0;i< a.length;i++){
System.out.print(a[i] + " ");
}
}

public static int[] quickSort(int a[],int left,int right) {
int dp;
if(left < right) {
dp = partition(a,left,right);
quickSort(a,left,dp - 1);
quickSort(a,dp + 1,right);
}
return a;
}

public static int partition(int a[], int left,int right) {

int temp;
int pivot = a[left];
while(left < right) {
while(left < right && a[right] > pivot) {
right--;
}
if(left < right) {
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
while(left < right && a[left] < pivot) {
left++;
}
if(left < right) {
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
a[left] = pivot;

}
return left;
}
}


8.归并排序

归并排序:
public class SortArray {
public static void main(String[] args) {
int a[] = {2,4,6,1,3,8,9,7,5};
mergeSort(a,0,a.length-1);
for(int i = 0;i < a.length;i++){
System.out.print(a[i] + " ");
}
}

public static int[] mergeSort(int a[],int low,int high) {
int mid = (low + high)/2;
if(low < high) {
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
merge(a,low,mid,high);
}
return a;
}

public static void merge(int a[],int low,int mid,int high) {
int array[] = new int[high - low +1];
//左指针
int i = low;
// 右指针
int j = mid + 1;
int k = 0;
// 把较小的数先移到新数组中
while(i <= mid && j <= high) {
if(a[i] < a[j]) {
array[k++] = a[i++];
}else {
array[k++] = a[j++];
}
}
// 把左边剩余的数移到数组
while(i <= mid) {
array[k++] = a[i++];
}
// 把右边剩余的数移到数组
while(j <= high) {
array[k++] = a[j++];
}
// 把新数组中的数覆盖a数组
for(int k2 = 0;k2 <array.length;k2++) {
a[k2 + low] = array[k2];
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: