部分排序算法c语言实现

代码比较粗糙,主要是用于对排序算法的理解,因而忽略了边界和容错处理相关代码。

 

相关文档:

Insert Sort  ,Bubble Sort ,Select Sort ,Shell sort ,Quick sort ,Heap sort ,Merge sort on Wikipedia

algorithm Repository :C语言实现部分排序算法,代码质量较高,其实现类似于库函数

sorting and searching algorithms :点击左边的链接即可查看整份文档

排序算法性能比较:图片链接

插入,选择,冒泡排序的算法复杂度为O(n^2)

希尔排序(shell sort)的算法复杂度因所采用增量序列的不同而有很大差别,例如shell增量序列(1,2,..,2^k)的算法复杂度可能达到O(n^2),其他增量序列则为O(n^1.5)到O(n^(7/6))不等,但其算法复杂度不可能达到O(nlogn);

快速排序,堆排序,归并排序算法复杂度为O(nlogn)。

快速排序虽然被认为是最快的,但是写一个完全正确的算法却并不容易(即在任何情况下算法复杂度均为O(nlogn)),感兴趣的可以看看glib 和bsd 的快速排序实现,有一篇论文《engineering a sort function 》中也写了qsort的实现

包含三个文件:

sort.c:

/* to compiler the program, use: gcc -o sort sort.c misc.c -g -Wall
/* insert sort */
#include <stdio.h>
#include <stdlib.h>
#include "misc.h"int insertSort(int a[], int n);
int shellSortSh(int a[], int n);
int shellSortHi(int a[], int n);
int shellSortKn(int a[], int n);
int bubSort(int a[], int n);
int selectSort(int a[], int n);
int median3(int a[], int n);
int quickSort(int a[], int n);
int heapify(int a[],int i, int n);
int heapSort(int a[], int n);
int mergeArray(int a[],int splitIndex,int n);
int mergeSort(int a[], int n);
/*
void testMedian3()
{int a[3] = {3,2,1};int len = ARRLEN(a);median3(a,len);printArray(a,len);}*/
int main(void)
{int a[] = {8,1,4,9,6,3,5,2,7,0};/*  int a[] = {5,7,3,8};*/int len = ARRLEN(a);/*  testSort(a,len,insertSort);*//*  testSort(a,len,shellSortKn);*/testSort(a,len,mergeSort);/*  testMedian3();*/return 0;
}
int insertSort(int a[], int n)
{int i,j;int tmp;for(i = 0; i < n; i++){tmp = a[i];for(j = i; j > 0 && a[j-1] > tmp; --j)a[j] = a[j-1];a[j] = tmp;/*      printArray(a,n);*/}return 0;
}
/* the origin shell sort by Shell using increment sequenceof n/2,n/4 ... 1 */
int shellSortSh(int a[], int n)
{int i,j;int inc;int tmp;for(inc = n/2; inc > 0; inc /= 2)for(i = inc; i <n; i++){tmp = a[i];for(j = i; j >= inc && tmp < a[j-inc]; j -= inc)a[j] = a[j-inc];a[j] = tmp;printArray(a,n);}return 0;
}
/* shell sort by Hibbard's sequence:2^k-1,...,7,3,1 */
int shellSortHi(int a[],int n)
{int i,j;int inc;int tmp;for(inc = 1; inc < n/2; inc = 2*inc+1) ;for( ; inc > 0; inc /= 2)for(i = inc; i <n; i++){tmp = a[i];for(j = i; j >= inc && tmp < a[j-inc]; j -= inc)a[j] = a[j-inc];a[j] = tmp;printArray(a,n);}return 0;
}
/* Shell sort using knuth's sequence:(3^k-1)/2,...,13,4,1 */
int shellSortKn(int a[], int n)
{int i,j;int inc;int tmp;for(inc = 1; inc < n/3; inc = 3*inc+1) ;for( ; inc > 0; inc /= 3)for(i = inc; i <n; i++){tmp = a[i];for(j = i; j >= inc && tmp < a[j-inc]; j -= inc)a[j] = a[j-inc];a[j] = tmp;printArray(a,n);}return 0;
}
/*for shell sort there is also a Sedgewick's sequence: 1,5,19,41,...which can be constructed by:1,19,104,505,...,9(4^k-2^k)+1, k=0,1,2,3,...5,41,209,929,...,(2^(k+2))*(2^(k+2)-3)+1, k = 0,1,2,3,.. *//*bubble sort */
int bubSort(int a[], int n)
{int i,j,tmp;for(i = n-1; i >= 0; --i)for(j = 0;  j < i; j++)if(a[j] >a[j+1]){tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;}return 0;
}
/* select sort */
int selectSort(int a[], int n)
{int i,j;int minIndex,minNum;for(i = 0; i < n; i++){minIndex = i;minNum = a[i];for(j = i+1; j < n; j++)if(a[j] < minNum){minIndex = j;minNum = a[j];}a[minIndex] = a[i];a[i] = minNum;}return 0;
}
/* partition function to find a element to cut an array to two pieces */
/* This function is used by quick sort function */
int median3(int a[], int n)
{int mid = n/2-1;/* the following three sentences make sure thata[0] <= a[mid] <= a[n-1] */if(a[0] > a[mid])swap(&a[0],&a[mid]);if(a[0] > a[n-1])swap(&a[0],&a[n-1]);if(a[mid] > a[n-1])swap(&a[mid],&a[n-1]);/* exchange elemments to set the pivot at the beginning of array */swap(&a[0],&a[mid]);return a[0];
}
/* quick sort */
int quickSort(int a[], int n)
{int low = 1, high = n-1;int pivot;printArray(a,n);if(n <= 3){insertSort(a,n);return 0;}pivot = median3(a,n);while(low < high){while(a[low] < pivot) low++;while(a[high] > pivot) high--;if(low < high)swap(&a[low],&a[high]);elsebreak;/*      printArray(a,10);*/}swap(&a[0],&a[low-1]);quickSort(a,low-1);quickSort(a+low,n-low);return 0;
}
int heapify(int a[], int i, int n)
{int maxIndex = i;int lChild = 2*i+1,rChild = lChild+1;if(lChild < n && a[lChild] > a[maxIndex])maxIndex = lChild;if(rChild < n && a[rChild] > a[maxIndex])maxIndex = rChild;if(maxIndex != i){swap(&a[maxIndex],&a[i]);heapify(a,maxIndex,n);}return 0;
}
int heapSort(int a[], int n)
{int i;for(i = (n-1)/2; i >= 0; i--)heapify(a,i,n);for(i = n-1; i >0; i--){swap(&a[i],&a[0]);heapify(a,0,--n);}return 0;
}
int mergeArray(int a[], int splitIndex, int n)
{int *tmpArray = malloc(n*sizeof(int));int i ,left,right;i = left= 0;right = splitIndex;while(left < splitIndex && right < n){if(a[left] <= a[right])tmpArray[i++] = a[left++];elsetmpArray[i++] = a[right++];}while(left < splitIndex)tmpArray[i++] = a[left++];while(right < n)tmpArray[i++] = a[right++];for(i = 0; i < n; i++)a[i] = tmpArray[i];return 0;
}
int mergeSort(int a[], int n)
{int mid;if(n > 1){mid = n/2;mergeSort(a,mid);mergeSort(a+mid,n-mid);mergeArray(a,mid,n);}return 0;
}
 

misc.c:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>void fillArray(int a[], int n)
{int i;srand(time(NULL));for(i = 0; i < n; i++)a[i] = rand();
}
void printArray(int a[], int n)
{int i;printf("%d",a[0]);for(i = 1; i < n; i++)printf(" %d",a[i]);printf("\n");
}
void testSort(int a[], int n, int (*sort)(int a[], int n))
{printf("the initial array is:\n");printArray(a,n);sort(a,n);printf("\nAfter sorting,the array is:\n");printArray(a,n);
}
void swap(int *x, int *y)
{int tmp = *x;*x = *y;*y = tmp;
}

 misc.h:

#ifndef MISC_H
#define MISC_H#define ARRLEN(a) (sizeof(a)/sizeof(a[0]))
void fillArray(int a[], int n);
void printArray(int a[], int n);
void testSort(int a[], int n, int (*sort)(int a[], int n));
void swap(int *x, int *y);
#endif
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546057.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机等级考试二级ACCESS考试大纲

公共基础知识部分30分 专业语言部分 70分 基本要求 1. 具有数据库系统的基础知识。 2. 基本了解面各对象的概念。 3. 掌握关系数据库的基本原理。 4. 掌握数据库程序设计方法。 5. 能使用Access建立一个小型数据库应用系统。 考试内容 一、 数据库基础知识 1. 基本概念&#xf…

C语言 常用单词

main 主要的 printf(print format)格式输出 include , return ,if ,else ,switch ,case 机箱&#xff1b;案例&#xff1b; default 默认 ,for while break 暂停&#xff1b;间断&#xff1b; continue math int char 字符&#xff1b; …

ipv6寻址_有类和无类寻址:IPV4寻址| 计算机网络

ipv6寻址1)分类寻址 (1) Classful Addressing) IPv4 addressing used the concept of classes. This architecture is known as classful addressing. IPv4寻址使用类的概念。 这种体系结构称为类寻址 。 In the classful addressing, there are 5 classes in which the addre…

windows没有软盘 怎么解决

1这种情况我遇到过。 现象为&#xff1a;启动快到桌面之前&#xff0c;会出现红叉错误提示框&#xff0c;标题为“Windows-没有软盘”&#xff0c;内容为“驱动器中没有软盘。请在\Device\Harddisk1\DR5 中插入软盘”&#xff0c;有“取消”、“重试”、“继续”三个按钮。点“…

forth编程语言

forth 是一种基于栈的程序设计语言&#xff0c;其语法使用逆波兰表达式&#xff08;即后缀表达式&#xff09;&#xff0c;forth的黄金期是上世纪80年代&#xff0c;现在使用的人很少&#xff0c;但是却非常的有趣。还有一个以forth为基础开发的语言factor ,它增加了许多当代的…

安装TPCC-MySQL报错

2019独角兽企业重金招聘Python工程师标准>>> 安装TPCC-MySQL做压力测试&#xff0c;由于TPCC-MySQL是bzr工具进行版本控制的&#xff0c;所以要先安装bzr [rootmha_backup /root] #rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.r…

enumerate_Java Thread类的static int enumerate(Thread [] th)方法与示例

enumerate线程类static int枚举(Thread [] th) (Thread Class static int enumerate(Thread[] th)) This method is available in package java.lang.Thread.enumerate(Thread[] th). 软件包java.lang.Thread.enumerate(Thread [] th)中提供了此方法。 This method is used to …

自由职业的前半年,我是如何度过的?

生活中所受的苦&#xff0c;终会以一种形式回归。我是一个后知后觉的人&#xff0c;从 2009 年毕业到现在&#xff0c;已经有 11 年的光景了&#xff0c;参加工作的前几年我从没想过要快速的提升自己的技能&#xff0c;对待工作也没有全力以赴&#xff0c;这样的迷茫和随大流的…

Microsoft Visual C++ Runtime Library 错误解决办法

今天安装软件时&#xff0c;出现“Microsoft Visual C Runtime Library”错误&#xff0c;网上查了下解决方法&#xff0c;得以解决。现在分享下&#xff0c;给碰到相同问题的朋友。微软官方解释如下&#xff1a;症状在 Microsoft Windows XP 中运行自定义 Microsoft Visual C …

Eucalyptus常用查询命令

前言&#xff1a; Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems &#xff08;Eucalyptus&#xff09; 是一种开源的软件基础结构&#xff0c;用来通过计算集群或工作站群实现弹性的、实用的云计算。它最初是美国加利福尼亚大学 Santa …

the blocks problem(uva 101 or poj 1208)

题目描述见&#xff1a;uva 101 or poj 1208 关键在于彻底理解题目中搬积木的几个命令的含义&#xff0c;见具体分析 如果还不能理解题意&#xff0c;那么找一个正确通过的代码&#xff0c;编译并输入测试数据&#xff0c;查看其每一个命令的执行情况。如我的代码中162行注…

调整灰度图像的大小,而无需在Python中使用任何内置函数

In this program, we will be using two functions of OpenCV-python (cv2) module. Lets see their syntax and descriptions first. 在此程序中&#xff0c;我们将使用OpenCV-python(cv2)模块的两个功能。 首先让我们看看它们的语法和说明。 1) imread():It takes an absolu…

第一章 认识计算机

*(%)^*&!*第一讲 了解计算机基础知识一、计算机的发展历程1、计算机的起源&#xff08;1&#xff09;世界上第一台计算机&#xff1a;1946年诞生&#xff0c;名称为ENIAC。&#xff08;2&#xff09;世界上第一台并行计算机&#xff1a;1950年诞生&#xff0c;名称为EDVAC&…

scoket多线程例子

大体思路&#xff0c;有n台mc&#xff0c;要dump出数据&#xff0c;n台进行对比&#xff0c;看数据是否一致&#xff0c;设计到同时dump的问题&#xff0c;server断发条指令给这n台mc&#xff0c;mc同时去dump把结果返回给server端&#xff0c;server端把这些结果进行对比serve…

csapp bufbomb实验

csapp (《深入理解计算机系统》&#xff09;一书中有一个关于缓冲区溢出的实验&#xff0c;其程序代码如下&#xff1a; /* Bomb program that is solved using a buffer overflow attack */#include <stdio.h> #include <stdlib.h> #include <ctype.h> #in…

漫画:对象是如何被找到的?句柄 OR 直接指针?

小贴士&#xff1a;想要使用并定位 Java 对象&#xff0c;就要用到 Java 虚拟机栈&#xff08;Java Virtual Machine Stack&#xff09;&#xff0c;它描述的是 Java 方法执行的线程内存模型&#xff1a;每个方法被执行的时候&#xff0c;Java 虚拟机都会同步创建一个栈帧&…

在C ++中检查一个数组是否是另一个数组的子数组

Prerequisite: std::equal() function 先决条件&#xff1a; std :: equal()函数 Problem statement: 问题陈述&#xff1a; Check if one array is subarray of another or not. 检查一个数组是否是另一个数组的子数组。 Example: 例&#xff1a; Input 1:Arr1 [3, 4, 5, …

第二章 认识计算机硬件

*(%)^*&!*第一讲 认识计算机主板一、主板的结构1、主板结构分类&#xff08;2&#xff09;AT、Baby-AT型&#xff08;2&#xff09;ATX型&#xff08;3&#xff09;Micro ATX板型&#xff08;4&#xff09;LPX、NLX、Flex ATX板型&#xff08;5&#xff09;EATX、WATX板型&…

IDEA 不为人知的 5 个骚技巧!真香!

工欲善其事&#xff0c;必先利其器&#xff0c;磊哥最近发现了几个特别棒的 IDEA“骚”技巧&#xff0c;已经迫不及待的想要分享给你了&#xff0c;快上车...1.快速补全行末分号使用快捷键 Shfit Ctrl Enter 轻松实现。2.自带的 HTTP 请求工具IDEA 自带了 HTTP 的测试工具&am…

教育编程语言(转)

这是wikipedia上的内容&#xff0c;转载保存&#xff0c;以便以后查阅&#xff0c;英文版见Educational programming language 主要是介绍了一些适合于教育的编程语言&#xff0c;分别适合于不同的个人需求。 详细内容如下&#xff1a; 许多教育性质的程序设计语言都提供建议…