数据结构的一些算法实现-创新互联
文章目录
当前题目:数据结构的一些算法实现-创新互联
标题链接:http://myzitong.com/article/eesdi.html
- 快速排序
- 插入排序
- 选择排序
- 希尔排序
- 二分查找
void CQuickSort::quickSort( int *p_val, int size )
{int t_baseNumber = *p_val;//枢轴数
int* head = p_val;
int* tail = p_val + size-1;
int temp = 0;
if (size<= 0)
{return;
}
while (head != tail)
{if (*head >*tail)//后面的数 小于枢轴数时 就进行交换
{ temp = *head;
*head = *tail;
*tail = temp;
}
if (*head == t_baseNumber)//每次对比的数都是枢轴数 所以应该拿枢轴数来比较
{ tail--;
}
else
{ head++;
}
}
quickSort(p_val, head - p_val);
quickSort(head+1, size -(head-p_val+1));
}
插入排序void CInsertSort::insertSort( int *p_val, int size )
{int temp = 0;
for (int index=1; index<= size -1; index++)//外层的循环控制用来排序的数有多少个
{for (int number= index; number >= 1; number--)//内层循环用来从后往前逐个对比,交换顺序
{ if (p_val[number]< p_val[number-1])
{ temp = p_val[number];
p_val[number] = p_val[number-1];
p_val[number-1] = temp;
}
else
{ break;
}
}
}
}
选择排序void CThreadAndAlgorithm::choice_sort( int *p_val, int size )
{int temp=0;
for (int i=size-1; i >= 1; i--)
{for (int j=0; j< i-1; j++)
{ if (p_val[j] >p_val[i])
{ temp = p_val[i];
p_val[i] = p_val[j];
p_val[j] = temp;
}
}
}
}
希尔排序void CThreadAndAlgorithm::shellSort( int *p_val, int size )
{int tmp = 0;
int step = size/2;
while (step)
{for (int i=step; i tmp = p_val[i];
int j=i;
while (j >= step && tmp p_val[j]= p_val[j-step];
j -= step;
}
p_val[j] = tmp;
}
step = step/2;
}
}
二分查找int CThreadAndAlgorithm::binarySearch( int *p_val, int size, int val )
{int* p_head = p_val;
int low =0;
int middle =0;
int height = size;
while(low< height)
{middle = (low + height)/2;
if (p_val[middle] == val)
{ return middle;
}
else if (p_val[middle] >val)
{ height = middle - 1;
}
else if (p_val[middle]< val)
{ low = middle + 1;
}
}
return -1;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前题目:数据结构的一些算法实现-创新互联
标题链接:http://myzitong.com/article/eesdi.html