【刷题经验】C/C++ 数组 排序 & 查找

文章目录

  • 1. 前言
  • 2. 排序
    • 2.1 二维整数数组(vector>)的排序
  • 3. 搜索(查找)
    • 3.1 二分查找
      • 3.1.1 Recursive implementation of Binary Search
      • 3.1.2 iterative implementation of Binary Search
      • 3.1.3 C++ STL binary_search
  • 0. 参考与引用

1. 前言

4个月没有碰键盘了,开学遥遥无期,偏偏电脑还在学校,取不得,真叫人蓝瘦。来到大姑家,一部12年前的台式机,登陆了久违的leetcode,一道简单的题目(56. 合并区间),但是C++语法点难住我了,大致思路是二维数组按照区间左端点从小到大排序,然后看排序后的每一个区间的右端点是否大于等于其右相邻区间的左端点,如果是则合并为一个区间并更新最终返回的ans二维数组。

现在的问题是二维数组的排序我忘记了,找了一遍CSDN博客没有类似的题目解析,于是我打算从此把以后所有关于C/C++排序的代码经验码到这篇文章,方便日后查阅。

还有一些搜索技巧和方法。


2. 排序

2.1 二维整数数组(vector>)的排序

如果是一个二维数组,也可以是用sort,我们可以选择根据某一列来进行排序,如果我们不重写cmp函数,那么默认的是根据第一列来排序,当然我们可以通过重写来根据其他列来排序:

/* Input matrix
m = [4 2 
8 3
5 1 ]
*/

// Ascending order by first column
sort(m.begin(), m.end());
/*
m = [8 3
4 2 
5 1 ]
*/

// Descending order by first column
sort(m.rbegin(), m.rend());
/*
m = [5 1
4 2 
8 3]
*/


// Ascending order by second column
sort(m.begin(), m.end(), [](const vector<int> &a, const vector<int> &b) { return a[1] < b[1]; } );

bool cmp(const vector<int> &a, const vector<int> &b) {
    return a[0] > b[0];
}
sort(m.begin(), m.end(), cmp);
/*
m = [4 2 
5 1
8 3 ]
*/


// Descending order by second column
sort(m.begin(), m.end(), [](const vector<int> &a, const vector<int> &b) { return a[1] > b[1]; } );

bool cmp(const vector<int> &a, const vector<int> &b) {
    return a[0] < b[0];
}
sort(m.begin(), m.end(), cmp);
/*
m = [8 3
5 1
4 2  ]
*/

3. 搜索(查找)

3.1 二分查找

数据序列的排序最主要的目的是为了方便查找。其中二分最常用。首先我们从算法的两种实现代码来一窥二分的魅力。

3.1.1 Recursive implementation of Binary Search

// C++ program to implement recursive Binary Search 
#include  
using namespace std; 

// A recursive binary search function. It returns 
// location of x in given array arr[l..r] is present, 
// otherwise -1 
int binarySearch(int arr[], int l, int r, int x) 
{ 
	if (r >= l) { 
		int mid = l + (r - l) / 2; 

		// If the element is present at the middle 
		// itself 
		if (arr[mid] == x) 
			return mid; 

		// If element is smaller than mid, then 
		// it can only be present in left subarray 
		if (arr[mid] > x) 
			return binarySearch(arr, l, mid - 1, x); 

		// Else the element can only be present 
		// in right subarray 
		return binarySearch(arr, mid + 1, r, x); 
	} 

	// We reach here when element is not 
	// present in array 
	return -1; 
} 

int main(void) 
{ 
	int arr[] = { 2, 3, 4, 10, 40 }; 
	int x = 10; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	int result = binarySearch(arr, 0, n - 1, x); 
	(result == -1) ? cout << "Element is not present in array"
				: cout << "Element is present at index " << result; 
	return 0; 
} 

3.1.2 iterative implementation of Binary Search

// C++ program to implement recursive Binary Search 
#include  
using namespace std; 

// A iterative binary search function. It returns 
// location of x in given array arr[l..r] if present, 
// otherwise -1 
int binarySearch(int arr[], int l, int r, int x) 
{ 
	while (l <= r) { 
		int m = l + (r - l) / 2; 

		// Check if x is present at mid 
		if (arr[m] == x) 
			return m; 

		// If x greater, ignore left half 
		if (arr[m] < x) 
			l = m + 1; 

		// If x is smaller, ignore right half 
		else
			r = m - 1; 
	} 

	// if we reach here, then element was 
	// not present 
	return -1; 
} 

int main(void) 
{ 
	int arr[] = { 2, 3, 4, 10, 40 }; 
	int x = 10; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	int result = binarySearch(arr, 0, n - 1, x); 
	(result == -1) ? cout << "Element is not present in array"
				: cout << "Element is present at index " << result; 
	return 0; 
} 

3.1.3 C++ STL binary_search

// binary_search example
#include      // std::cout
#include     // std::binary_search, std::sort
#include        // std::vector

bool myfunction (int i,int j) { return (i<j); }

int main () {
  int myints[] = {1,2,3,4,5,4,3,2,1};
  std::vector<int> v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1

  // using default comparison:
  std::sort (v.begin(), v.end());

  std::cout << "looking for a 3... ";
  if (std::binary_search (v.begin(), v.end(), 3))
    std::cout << "found!\n"; else std::cout << "not found.\n";

  // using myfunction as comp:
  std::sort (v.begin(), v.end(), myfunction);

  std::cout << "looking for a 6... ";
  if (std::binary_search (v.begin(), v.end(), 6, myfunction))
    std::cout << "found!\n"; else std::cout << "not found.\n";

  return 0;
}

0. 参考与引用

  • C++ sort vector or vector 容器的排序
  • 二分查找 两种实现
  • 二分查找 C++ STL

你可能感兴趣的:(C/C++,刷题)