當前位置:首頁 » 編程軟體 » 插入排序編譯代碼

插入排序編譯代碼

發布時間: 2024-12-05 09:41:40

『壹』 在鍵盤上輸入10個元素,利用插入法降序排列,現輸入一個數x,要求插入到數組中,插入後仍按原規律排列。

給,已經編譯運行確認:

#include "stdio.h"
void sort(int *p)
{
int i,j,k;
for (i=0;i<9;i++)
for (j=i+1;j<10;j++)
if (p[i]<p[j])
{
k=p[i];
p[i]=p[j];
p[j]=k;
}
}

void main()
{
int a[11],i,j,x;

printf("請輸入10個數: \n");
for (i=0;i<10;i++)
{
printf("第%d個數: ",i+1);
scanf("%d",&a[i]);
}

sort(a);

printf("排序後的數組: \n");
for (i=0;i<10;i++)
printf("%d ",a[i]);

printf("\n請輸入要插入的數x: \n");
scanf("%d",&x);

for(i=0;i<10;i++)
{
if(x>a[i])
{
for(j=10;j>i;j--)
{
a[j]=a[j-1];
}

a[i]=x;
break;
}
}

printf("插入後的數組: \n");
for (i=0;i<11;i++)
printf("%d ",a[i]);
}

『貳』 數據結構實現折半插入排序(c語言版)

# include <stdio.h>#define Max 7
void B_sort(int a[], int n)
{
int low,high, i,j, t;
int m;
for(i=2;i<=n;++i)
{
t = a[i];//臨時存放
low =1;high =i-1;
while(low<=high)
{
m=(low+high)/2;
if(t<a[m])
high =m-1;
else
low =m+1;
}
for(j=i-1;j>=high+1;--j)

{
a[j+1] = a[j];
}

a[high+1] = t;
}}
main()
{int a[Max],i;
int length = Max-1;
printf(" 這是一個折半排序 \n");
printf("請輸入%d個待排序的記錄序列:\n",length-1);
for(i=1;i<length;i++)
scanf("%d",&a[i]);
B_sort(a,length);
printf("折半排序後的序列:");
for(i=2;i<=length;i++)
printf(" %d",a[i]);
printf("\n");
}

vc下面編譯通過的,折半插入排序,希望採納

『叄』 怎麼插入20個數,按順序列印出來(用數據結構做)

給,已經編譯運行確認了:
使用的是數據結構中的插入排序演算法

#include<conio.h>
#include<stdio.h>

void main()
{
int a[20]={NULL};
int i,j;
int temp;

printf("請輸入20個數: \n");
for(i=0;i<20;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}

for(i=2;i<20;i++)
{temp=a[i];
j=i-1;
while(j>=0&&temp<a[j])
{a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}

printf("排序後: \n");
for(i=0;i<20;i++)
printf("%d ",a[i]);

getch();
}

熱點內容
泰拉瑞亞蒲公英怎麼開在線伺服器 發布:2025-03-14 14:21:20 瀏覽:629
如何破壞門上的密碼鎖 發布:2025-03-14 14:19:39 瀏覽:968
咖啡源碼 發布:2025-03-14 13:51:32 瀏覽:168
android漂亮的listview 發布:2025-03-14 13:40:26 瀏覽:392
android路線規劃 發布:2025-03-14 13:23:22 瀏覽:304
poi瀏覽器島風go緩存 發布:2025-03-14 13:10:24 瀏覽:189
具體可要說存儲在鋼瓶中是因為 發布:2025-03-14 13:00:36 瀏覽:442
汽車空調壓縮機不轉了 發布:2025-03-14 12:55:45 瀏覽:32
安卓和平營地cp怎麼組 發布:2025-03-14 12:55:40 瀏覽:606
時序模式演算法 發布:2025-03-14 12:50:45 瀏覽:205