ACM——直接插入法排序

來源:互聯網
上載者:User

標籤:style   blog   java   color   strong   資料   

NOJ——1062直接插入排序

時間限制(普通/Java):1000MS/3000MS          運行記憶體限制:65536KByte
總提交:446            測試通過:212

描述

給定輸入排序元素數目n和相應的n個元素,寫出程式,利用內排序演算法中直接插入排序演算法進行排序,並輸出排序過程中每趟及最後結果的相應序列。

輸入

共兩行,第一行給出排序元素數目n,第二行給出n個元素,1≤n≤400,每個元素值範圍為 [0,100000)

輸出

三個部分:第1部分為兩行,第1行輸出文字“Source:”,第2行給出原始序列;第2部分,開始輸出文字“Insert Sort:”,後續輸出簡單選擇排序過程;

第3部分,開始輸出文字“Result:”,後續輸出排序結果。

範例輸入

7
48 36 68 72 12 48 2

範例輸出

Source:
(48) 36 68 72 12 48 2
Insert Sort:
(36 48) 68 72 12 48 2
(36 48 68) 72 12 48 2
(36 48 68 72) 12 48 2
(12 36 48 68 72) 48 2
(12 36 48 48 68 72) 2
(2 12 36 48 48 68 72)
Result:
(2 12 36 48 48 68 72)

提示

 資料結構A實驗四

題目來源

CHENZ

 

#include<iostream>#include<stdio.h>using namespace std;int main(){    int N,i,j;    scanf("%d",&N);    int* p=new int[N];    for(i=0;i<N;i++)        scanf("%d",&p[i]);    printf("Source:\n(%d)",p[0]);    for(i=1;i<N;i++)    printf(" %d",p[i]);    printf("\nInsert Sort:\n");    for(i=1;i<N;i++){        if(p[i]<p[i-1])        {        int temp=p[i];        for(j=i-1;j>=0&&temp<p[j];j--){        p[j+1]=p[j];        }        p[j+1]=temp;        }        printf("(%d",p[0]);        for(int t=1;t<=i;t++) printf(" %d",p[t]);        printf(")");        for(int t=i+1;t<N;t++) printf(" %d",p[t]);        printf("\n");    }    printf("Result:\n(%d",p[0]);    for(i=1;i<N;i++)    printf(" %d",p[i]);    printf(")\n");        return 0;}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.