数据结构应用标准模版库——Sorting

1 sort:

int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);
sort(A, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The output is " 1 2 4 5 7 8".

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
	return a <b;
}
int main()
{
	int arr[]={20,35,70,45,6,88,14,100,2,1212,5};
	int arrsize=sizeof(arr)/sizeof(int);
	sort(arr,arr+arrsize,cmp);
	copy(arr,arr+arrsize,ostream_iterator<int>(cout," "));
//	for(int i=0;i<arrsize;i++)
//		cout<<arr[i]<<' ';
	cout<<endl;
	return 0;
}



2 stable_sort:

inline bool lt_nocase(char c1, char c2) { return tolower(c1) < tolower(c2); }

int main()
{
  char A[] = "fdBeACFDbEac";
  const int N = sizeof(A) - 1;
  stable_sort(A, A+N, lt_nocase);
  printf("%s\n", A);
  // The printed result is ""AaBbCcdDeEfF".
}

#include<iostream>
#include<algorithm>
using namespace std;
inline bool cmp(int a,int b)
{
	return a <b;
}
int main()
{
	int arr[]={20,35,70,45,6,88,14};
	int arrsize=sizeof(arr)/sizeof(int);
	stable_sort(arr,arr+arrsize,cmp);
	for(int i=0;i<arrsize;i++)
		cout<<arr[i]<<' ';
	return 0;
}


3 partial_sort:  部分排序,不是全序排序

int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);

partial_sort(A, A + 5, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The printed result is "1 2 3 4 5 11 12 10 9 8 7 6".


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int arr[]={20,35,70,45,6,88,14,100,2,1,5};
	int arrsize=sizeof(arr)/sizeof(int);
	partial_sort(arr,arr+6,arr+arrsize);
	copy(arr,arr+arrsize,ostream_iterator<int>(cout," "));
//	for(int i=0;i<arrsize;i++)
//		cout<<arr[i]<<' ';
	cout<<endl;
	return 0;
}


4 partial_sort_copy:

int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);

vector<int> V(4);
partial_sort_copy(A, A + N, V.begin(), V.end());
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
// The printed result is "1 2 3 4"

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	vector<int> v(4);

	int arr[]={20,35,70,45,6,88,14,100,2,1,5};
	const int arrsize=sizeof(arr)/sizeof(int);
	partial_sort_copy(arr,arr+arrsize,v.begin(),v.end());
	copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
//	for(int i=0;i<arrsize;i++)
//		cout<<arr[i]<<' ';
	cout<<endl;
	return 0;
}


5 merge:

int main()
{
  int A1[] = { 1, 3, 5, 7 };
  int A2[] = { 2, 4, 6, 8 };
  const int N1 = sizeof(A1) / sizeof(int);
  const int N2 = sizeof(A2) / sizeof(int);

  merge(A1, A1 + N1, A2, A2 + N2, 
        ostream_iterator<int>(cout, " "));
  // The output is "1 2 3 4 5 6 7 8"
}

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int arr0[]={1,3,5,7,66,77,88,44,21,33};
	int arr1[]={20,35,70,45,6,88,14,100};
	const int arrsize0=sizeof(arr0)/sizeof(int);
	const int arrsize1=sizeof(arr1)/sizeof(int);
	merge(arr0,arr0+arrsize0,arr1,arr1+arrsize1,ostream_iterator<int> (cout," "));
//	for(int i=0;i<arrsize;i++)
//		cout<<arr[i]<<' ';
	cout<<endl;
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值