⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bidir_bsort.txt

📁 从头和尾同时进行冒泡排序
💻 TXT
字号:
//**************************************
//     
// Name: one way& bidirectional bubble s
//     ort
// Description:This program accomplishes
//     a bubble sort in two directions.Fron the
//     start as well as the end of the array li
//     st.This code also demonstrates the one w
//     at bubble sort and compares their effici
//     ences.
// By: Nikhil.N
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.1CPlusPlusStreet.com/xq/ASP/txtCodeI
//     d.1598/lngWId.3/qx/vb/scripts/ShowCode.h
//     tm//for details.//**************************************
//     

#include<stdio.h>
#define MAX 10
int a[MAX],b[MAX];
void main()


    {
    int n,i,j,pass,sw=1,temp;
    clrscr();
    printf("Enter the number of elements present.\n");
    scanf("%d",&n);
    printf("Enter the elements .\n");
    for(i=0;i<n;i++)


        {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    printf("The array elements before sorting are\n");
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
    printf("Array elements after one way bubble sorting are\n");
    for(i=0;i<n-1&&sw==1;i++)


        {
        sw=0;
        for(j=0;j<n-1-i;j++)
        if(a[j]>a[j+1])


            {
            temp=a[j+1];
            a[j+1]=a[j];
            a[j]=temp;
            sw=1;
        }
    }
    for(j=0;j<n;j++)
    printf("%d\n",a[j]);
    printf("NO. of passes - %d\n",i);
    printf("Elements after 2 way bubble sort\n");
    sw=1;
    for(i=0;i<n-1 && sw==1;i++)


        {
        sw=0;
        for(j=0;j<n-1-i;j++)


            {
            if(b[j]>b[j+1])


                {
                temp=b[j+1];
                b[j+1]=b[j];
                b[j]=temp;
                sw=1;
            }
            if(b[n-1-j]<b[n-2-j])


                {
                temp=b[n-2-j];
                b[n-2-j]=b[n-1-j];
                b[n-1-j]=temp;
                sw=1;
            }
        }
    }
    for(j=0;j<n;j++)
    printf("%d\n",b[j]);
    printf("No. of passes %d\n",i);
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -