#include <iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int Search(int arr[], int key)
{
for (int i = 1; i <= arr[0]; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
void printArr(int arr[])
{
for (int i = 1; i <= arr[0]; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void BubbleSort(int arr[]){
int temp = arr[1];
int count = 0;
for (int i = arr[0]; i>1; i--){
for (int j = 1; j<i; j++){
if (arr[j]>arr[j + 1]){
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
count++;
}
}
}
cout << "swap=" << count << endl;
}
void InserSort(int arr[])
{
if (arr[0] <= 1)
return;
for (int i = 2; i <= arr[0]; i++)
{
int temp = arr[i];//关键点
if (arr[i]<arr[i - 1])
{
int j = i - 1;
for (; j>0 && arr[j]>temp; j--)//关键点
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
return;
}
int BinarySearch(int arr[], int key)
{
int low, high, mid;
low = 1; high = arr[0];
while (low <= high)
{
mid = (low + high) / 2;
if (key == arr[mid])
return mid;
else if (key<arr[mid])
high = mid - 1;
else low = mid - 1;
}
return -1;
}
int Partition(int arr[], int low, int high)
{
int temp = arr[low];
int pivotkey = arr[low];
while (low<high)
{
while (low<high&&arr[high] >= pivotkey)
--high;
arr[low] = arr[high];
while (low<high&&arr[low] <= pivotkey)
++low;
arr[high] = arr[low];
}
arr[low] = temp;
return low;
}
void QSort(int arr[], int low, int high)
{
int pivotloc;
while (low<high)
{
pivotloc = Partition(arr, low, high);
QSort(arr, low, pivotloc - 1);
QSort(arr, pivotloc + 1, high);
}
}
int QuickSort(int arr[])
{
QSort(arr, 1, arr[0]);
return 1;
}
int main()
{
int key = 25;
int arr[11] = { 10, 3, 25, 11, 14, 15, 27, 19, 20, 29, 30 };
/*cout<<"begin print"<<endl;
cout<<Search(arr,key)<<endl;
InserSort(arr);
printArr(arr);*/
cout<<"begin print"<<endl;
cout<< BinarySearch(arr,key)<<endl;
BinarySearch(arr,key);
printArr(arr);
/*cout << "begin print" << endl;
cout<<Partition(arr,key)<<endl;
QuickSort(arr);
printArr(arr);*/
while (true);
return 0;
}
排序结果输不出来是什么问题
#include<ctime>
#include<cstdlib>
using namespace std;
int Search(int arr[], int key)
{
for (int i = 1; i <= arr[0]; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
void printArr(int arr[])
{
for (int i = 1; i <= arr[0]; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void BubbleSort(int arr[]){
int temp = arr[1];
int count = 0;
for (int i = arr[0]; i>1; i--){
for (int j = 1; j<i; j++){
if (arr[j]>arr[j + 1]){
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
count++;
}
}
}
cout << "swap=" << count << endl;
}
void InserSort(int arr[])
{
if (arr[0] <= 1)
return;
for (int i = 2; i <= arr[0]; i++)
{
int temp = arr[i];//关键点
if (arr[i]<arr[i - 1])
{
int j = i - 1;
for (; j>0 && arr[j]>temp; j--)//关键点
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
return;
}
int BinarySearch(int arr[], int key)
{
int low, high, mid;
low = 1; high = arr[0];
while (low <= high)
{
mid = (low + high) / 2;
if (key == arr[mid])
return mid;
else if (key<arr[mid])
high = mid - 1;
else low = mid - 1;
}
return -1;
}
int Partition(int arr[], int low, int high)
{
int temp = arr[low];
int pivotkey = arr[low];
while (low<high)
{
while (low<high&&arr[high] >= pivotkey)
--high;
arr[low] = arr[high];
while (low<high&&arr[low] <= pivotkey)
++low;
arr[high] = arr[low];
}
arr[low] = temp;
return low;
}
void QSort(int arr[], int low, int high)
{
int pivotloc;
while (low<high)
{
pivotloc = Partition(arr, low, high);
QSort(arr, low, pivotloc - 1);
QSort(arr, pivotloc + 1, high);
}
}
int QuickSort(int arr[])
{
QSort(arr, 1, arr[0]);
return 1;
}
int main()
{
int key = 25;
int arr[11] = { 10, 3, 25, 11, 14, 15, 27, 19, 20, 29, 30 };
/*cout<<"begin print"<<endl;
cout<<Search(arr,key)<<endl;
InserSort(arr);
printArr(arr);*/
cout<<"begin print"<<endl;
cout<< BinarySearch(arr,key)<<endl;
BinarySearch(arr,key);
printArr(arr);
/*cout << "begin print" << endl;
cout<<Partition(arr,key)<<endl;
QuickSort(arr);
printArr(arr);*/
while (true);
return 0;
}
排序结果输不出来是什么问题