# 一个折半插入排序差点写死Q

2018/11/24 09:40

//

//  main.c

//  binaryinsert

//

//  Created by mrruan on 2018/11/23.

//  Copyright © 2018 mrruan. All rights reserved.

//

#include <stdio.h>

typedef struct {

int data[10];

int length;

}SqList;

void binaryInsert(SqList *q){

int len = q->length;

int low,mid = 0,high;

int i,j;

int tmp;

for(i=1; i <= len; i++){

low = 0;

high = i-1;

j = i;

tmp = q->data[j];

while(low <= high){

mid = (low + high)/2;

if(tmp >= q->data[mid]){

low = mid+1;

}else{

high = mid-1;

}

}//while

//插入j的元素到前面位置

for(j=i; j > high; j--){

q->data[j] = q->data[j-1];

}

q->data[mid] = tmp;

}

printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",q->data[0],q->data[1],q->data[2],q->data[3],q->data[4],q->data[5],q->data[6],q->data[7],q->data[8],q->data[9]);

}

int main(void) {

SqList *L;

L = (SqList *)malloc(sizeof(SqList));

L->data[0] = 10;

L->data[1] = 9;

L->data[2] = 8;

L->data[3] = 7;

L->data[4] = 6;

L->data[5] = 5;

L->data[6] = 3;

L->data[7] = 2;

L->data[8] = 44;

L->data[9] = 0;

L->length = 10;

binaryInsert(L);

printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",L->data[0],L->data[1],L->data[2],L->data[3],L->data[4],L->data[5],L->data[6],L->data[7],L->data[8],L->data[9]);

printf("Hello, World!\n");

return 0;

}



0
0 收藏

0 评论
0 收藏
0