摘要:1 #include "stdio.h" 2 #include "stdlib.h" 3 4 typedef int ElemType; 5 6 typedef struct node { 7 ElemType data; 8 struct node *next; 9 } *LNode, *LinkList; 10 11 // 初始化一个链表 12 LinkList 13 initLinkList(int n) { 14 LinkList list = NULL; 15 ElemType e; 16 LNode p,r; 17 ...
阅读全文
摘要:1 #include "stdio.h" 2 #include "stdlib.h" 3 4 #define MAXSIZE 10 5 6 typedef int ElemType; 7 typedef struct { 8 int *elem; 9 int length;10 int listsize;11 } Sqlist;12 13 // 初始化顺序表14 void15 initSqlist(Sqlist *L) {16 L->elem = (int *) malloc(MAXSIZE * sizeof(ElemType));17 if(!L
阅读全文
摘要:1 <?php 2 /** 3 * 快速排序 quick sort 4 * 5 **/ 6 7 function sort_quick($arrData) { 8 if(empty($arrData) || !is_array($arrData)) return false; 9 10 $flag = $arrData[0];11 $len = count($arrData) - 1;12 if($len == 0) return $arrData; // 如果只有一个数据的数组直接返回13 14 $arrLeft = array();1...
阅读全文
摘要:1 <?php 2 /** 3 * 查找 4 * 5 **/ 6 // 顺序查找 7 function normal_search($arrData,$val) { 8 $len = count($arrData); 9 if($len == 0) return -1;10 for($i = 0;$i < $len; $i++ ) {11 echo "find No.",$i + 1," value = ",$arrData[$i]," is = ",$val,"? <br/>";12 //
阅读全文
摘要:1 <?php 2 /** 3 * 简单选择排序 simple selection sort 4 * 5 * 原理: 一次选定数组中的每一个数,记下当前位置并假设它是从当前位置开始后面数中的最小数min=i,从这个数的下一个数开始扫描直到最后一个数,并记录下最小数的位置min,扫描结束后如果min不等于i,说明假设错误,则交换min与i位置上数。 6 */ 7 function sort_simple_selection($list) 8 { 9 $len=count($list);10 if(empty($len)) return$list;11 12 for($i=0;$i<$
阅读全文
摘要:1 <?php 2 /** 3 * 半折插入排序 straight binary sort 4 * 5 * 原理:当直接插入排序进行到某一趟时,对于 r[i] 来讲,前边 i-1 个记录已经按关键字有序。此时不用直接插入排序的方法,而改为折半查找,找出 r[i] 应插的位置,然后插入。 6 */ 7 function sort_binary_insertion($list) 8 { 9 $len=count($list);10 if(empty($len)) return$list;11 12 for($i=1; $i<$len; $i++)13 {14 $temp=$lis...
阅读全文
摘要:1 <?php 2 /** 3 * 冒泡排序 bubble sort 4 * 5 * 原理:多次循环进行比较,每次比较时将最大数移动到最上面。每次循环时,找出剩余变量里的最大值,然后减小查询范围。这样经过多次循环以后,就完成了对这个数组的排序 6 */ 7 function sort_bubble($list) 8 { 9 $len=count($list);10 if(empty($len)) return$list;11 $is_change=false;12 13 for($i=0;$i<$len; $i++)14 {15 for($j=$i+1; $j<$len; $
阅读全文
摘要:1 <?php 2 /** 3 * 三元组 Triplet 4 * 5 */ 6 class Triplet 7 { 8 private$_data=null; 9 10 // 初始化三元组11 publicfunction init($val1,$val2,$val3)12 {13 $this->_data[0] =$val1;14 $this->_data[1] =$val2;15 $this->_data[2] =$val3;16 returntrue;17 }18 19 // 销毁三元组20 publicfunction destroy()21 {22 ...
阅读全文