数据结构-实验六排序

hu.Hakim hu.Hakim     2022-08-26     237

关键词:

实验六   排序

 

实验目的

1、排序的基本概念

      1.掌握在数组上进行各种排序的方法和算法。

      2.深刻理解各种方法的特点,并能灵活应用。

      3.加深对排序的理解,逐步培养解决实际问题的编程能力。

实验内容

1、排序的基本概念

(一)基础题

     1.编写各种排序方法的基本操作函数:

    (1)s_sort(int e[],int n)选择排序;

    (2)si_sort(int e[],int n)直接插入排序;

(3)sb_sort(int e[],int n)冒泡排序;

(4)merge(int e[],int n)二路合并排序。

     2.调用上述函数实现下列操作:

(1)对于给定的数组E[N]={213,111,222,77,400,300,987,1024,632,555}调用选择排序函数进行排序;

(2)调用直接插入函数进行排序;

(3)调用冒泡排序函数进行排序;

(4)调用二路合并排序函数进行排序。

(二)提高题

1.编写希尔排序函数对给定的数组进行排序。

2.编写快速排序函数对给定的数组进行排序。

实验结果

1、排序的基本概念

(一)基础题

(1)画出数据结构基本运算的流程图

 

 
   

 

 

 

 

 

 

 

 

(2)程序运行主要结果截图

 

 

 

 

(3)程序源代码

#include<stdio.h>

#include<conio.h>

#define N 10

int E[N]={213,111,222,77,400,300,987,1024,632,555};

void s_sort(int e[],int n)

{

    int i,j,k,t;

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

    {

        for(k=i,j=i+1;j<n;j++)

            if(e[k]>e[j])

            k=j;

        if(k!=i)

        {

            t=e[i];

            e[i]=e[k];

            e[k]=t;

        }

    }

}

int main()

{

    int i;

    printf("顺序排序,初始数据序列为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    s_sort(E,N);

    printf(" 排序后的数据结构为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    getch();

 

}

#include<stdio.h>

#include<conio.h>

#define N 10

int E[N]={213,111,222,77,400,300,987,1024,632,555};

void si_sort(int e[],int n)

{

    int i,j,t;

    for(i=1;i<n;i++)

    {

        for(t=e[i],j=i-1;j>=0&&t<e[j];j--)

            e[j+1]=e[j];

        e[j+1]=t;

    }

}

int  main()

{

    int i;

    printf("直接排序,初始化数据为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    si_sort(E,N);

    printf(" 排序后的数据结构为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    getch();

}

#include<stdio.h>

#include<conio.h>

#define N 10

int E[N]={213,111,222,77,400,300,987,1024,632,555};

void sb_sort(int e[],int n)

{

    int j,p,h,t;

    for(h=n-1;h>0;h--)

    {

        for(p=j=0;j<h;j++)

            if(e[j]>e[j+1])

        {

            t=e[j];

            e[j]=e[j+1];

            e[j+1]=t;

            //p=count2;

        }

    }

}

int  main()

{

    int i;

    printf("冒泡排序,初始化数据序列为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    sb_sort(E,N);

    printf(" 排序后的数据结构序列为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    getch();

}

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#define N 10

int E[N]={213,111,222,77,400,300,987,1024,632,555};

void merge_step(int e[],int a[],int s,int m,int n)

{

    int i,j,k;

    k=i=s;

    j=m+1;

    while(i<=m&&j<=n)

        if(e[i]<=e[j])

        a[k++]=e[i++];

    else

        a[k++]=e[j++];

    while(i<=m)

        a[k++]=e[i++];

    while(j<=n)

        a[k++]=e[j++];

}

void merge_pass(int e[],int a[],int n,int len)

{

    int f_s,s_end;

    f_s=0;

    while(f_s+len<n)

    {

        s_end=f_s+f_s+2*len-1;

        if(s_end>=n)

            s_end=n-1;

        merge_step(e,a,f_s,f_s+len-1,s_end);

        f_s=s_end+1;

    }

    if(f_s<n)

        for( ;f_s<n;f_s++)

    a[f_s]=e[f_s];

}

void merge(int e[],int n)

{

    int *p,len=1,f=0;

    p=(int *)malloc(n*sizeof(int));

    while(len<n)

    {

        if(f==0)

            merge_pass(e,p,n,len);

        else

            merge_pass(p,e,n,len);

        len*=2;

        f=1-f;

    }

    if(f==1)

        for(f=0;f<n;f++)

        e[f]=p[f];

    free(p);

}

int main()

{

    int i;

    printf("归并排序,初始化数据序列为: ");

    for(i=000;i<N;i++)

        printf("%d ",E[i]);

    merge(E,N);

    printf(" 排序后的数据序列为: ");

    for(i=0;i<N;i++)

        printf("%d ",E[i]);

    getch();

}

(二)提高题

(1)画出数据结构基本运算的流程图

                                                  

     
     
 
   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2)程序运行主要结果截图

 

 

(3)程序源代码

#include <stdio.h>

#include <stdlib.h>

void ShellSort(int a[], int length)

{

    int increment;

    int i,j;

    int temp;

    for(increment=length/2;increment>0;increment/=2)

    {

        for(i=increment;i<length;i++)

        {

            temp=a[i];

            for(j=i-increment;j>=0&&temp<a[j];j-=increment)

            {

                a[j+increment]=a[j];

            }

            a[j+increment]=temp; //将第一个位置填上,记录后移

        }

    }

}

int main()

{

    int i, j;

    int a[]={213,111,222,77,400,300,987,1024,632,555};

    printf("初始化的数据序列为: ");

    for(i=0;i<10;i++)

    {

        printf("%d ",a[i]);

    }

    ShellSort(a,10);

    printf(" 排序后的序列是: ");

    for(j=0;j<10;j++)

    {

        printf("%d ", a[j]);

    }

    printf(" ");

    return 0;

}

#include<stdio.h>

#include<stdlib.h>

#define MAXNUM 10

#define keytype int

typedef struct

{

    keytype key;

}datatype;

datatype R[MAXNUM];

//datatype R[MAXNUM]={213,111,222,77,400,300,987,1024,632,555};

void read()

{

    int i,n;

    printf("请输入要输入的个数: ");

    scanf("%d",&n);

    printf("请输入初始排序数据: ");

    for(i=1;i<=n;i++)

    {

        scanf("%d",&R[i]);

    }

}

int Partition(datatype R[],int low,int high)

{

    R[0]=R[low];

    while(low<high)

    {

        while(low<high&&R[high].key>=R[0].key)

            high--;

        if(low<high)

        {

            R[low]=R[high];

            low++;

        }

        while(low<high&&R[low].key<R[0].key)

            low++;

        if(low<high)

        {

            R[high]=R[low];

            high--;

        }

    }

    R[low]=R[0];

    return low;

}

void Quick_Sort(datatype R[],int s,int t)

{

    int i;

    if(s<t)

    {

        i=Partition(R,s,t);

        Quick_Sort(R,s,i-1);

        Quick_Sort(R,i+1,t);

    }

}

int main()

{

 

    int i,s=1,t=10;

    read();

    /*printf("初始化数据为: ");*/

   Quick_Sort(R,s,t);

   printf(" 快速排序数据为: ");

   for(i=1;i<=MAXNUM;i++)

    printf("%d ",R[i]);

}

sdut3403数据结构实验之排序六:希尔排序(代码片段)

 数据结构实验之排序六:希尔排序TimeLimit: 1000ms MemoryLimit: 65536KiBProblemDescription我们已经学习了各种排序方法,知道在不同的情况下要选择不同的排序算法,以期达到最好的排序效率;对于待排序数据来说,若数据... 查看详情

c实验报告六(代码片段)

...2020.05.07一、实验目的与要求1、巩固学生对一维数组这种数据结构的理解,增强其程序设计能力2、巩固学生对二维数组这种数据结构的理解二、实验内容1、实验练习:7.3.1-1冒泡排序法排序1问题的简单描述:编写程序,利用随 查看详情

实验六(代码片段)

 Part1:编程练习1:补足程序学生的记录由学号和成绩组成。N名学生的数据已在主函数中放入结构体数组stu中。编写函数?ndMinlist,实现:把分数低的学生数据放在数组t中,函数返回分数低的学生的人数。(注意:分数低的学生可... 查看详情

20192304实验六《数据结构和面向对象的程序设计》实验报告(代码片段)

201923042020-2021-1《数据结构与面向对象程序设计》实验五报告课程:《程序设计与数据结构》班级:1923姓名:刘润衡学号:20192304实验教师:王志强实验日期:2020年11月5日必修/选修:必修1.实验内容(1)通过键盘输入一些整数,... 查看详情

实验六(代码片段)

Part1:结构体类型及编程应用补足程序ex1_2.cpp,附上补足后的程序源码,输入不同测试数据,给出运行结果截图#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函... 查看详情

实验六(代码片段)

【实验结论】Part1:结构体类型及编程应用•程序源码ex1_2.cpp1#include<stdio.h>23constintN=5;45//定义结构体类型structstudent,并定义STU为其别名6typedefstructstudent7longno;8charname[20];9intscore;10STU;1112//函数声明13voidinput(STU 查看详情

实验六(代码片段)

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstru 查看详情

实验六(代码片段)

第一部分①补足程序学生的记录由学号和成绩组成。N名学生的数据已在主函数中放入结构体数组stu中。编写函数findMinlist,实现:把分数最低的学生数据放在数组t中,函数返回分数最低的学生的人数。(注意:分数最低的学生可能... 查看详情

实验六(代码片段)

Part1:结构体类型及编程应用程序ex1_2.cpp代码如下:#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(S 查看详情

实验六(结构体与共用体)(代码片段)

part1:补足程序1.#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);v 查看详情

实验六进程基础

实验六进程基础 项目内容这个作业属于哪个课程2021春季Linux系统与应用这个作业的要求在哪里实验六进程基础学号-姓名18041519-黄健作业学习目标掌握Linux系统环境C语言编程概念、学习Linux系统进程概念实验内容1.请举例说明... 查看详情

实验六(代码片段)

  Part1:结构体类型及编程应用1,验证性实验学生成绩信息包括学号、姓名、考试课程、平时成绩、期中成绩、期末成绩、总评成绩和等级(优:90-100、良:80-89、中:70-79、及格:60-69、不及格:<60)。建立一个描述学生某... 查看详情

实验六(代码片段)

Part1 ex1-2在结构体数组stu中查找最低分,查找成绩与最低分相同的学生,将其信息保存在数组t中,返回最低分学生个数。#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];... 查看详情

数据库原理实验六——odbc/jdbc数据库编程(代码片段)

实验目的熟练掌握ODBC数据库访问技术和编程方法。掌握除ODBC外的其他数据库访问编程技术。实验内容这个实验首先需要配置ODBC数据源,以下是配置的流程首先在MySQL官网下载数据源驱动程序:下载地址注意选择64bit的... 查看详情

实验六(代码片段)

part1结构体类型与编程应用#include<stdio.h>constintN=5;typedefstructstudentlongno;charname[20];intscore;STU;voidinput(STUs[],intn);intfindminlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intmain()STU 查看详情

实验六(代码片段)

part1.#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidout 查看详情

实验六(代码片段)

part1.#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudent longno; charname[20]; intscore; STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voi 查看详情

实验六(代码片段)

part1ex1constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intm 查看详情