C++ STL sort()

std::sort

sorting algorithm is already implemented in C++ library
Defined in header algorithm
C ++ std::sort library sorts the data in ascending order by index order by default

template< class RandomIt, class Compare >
constexpr void sort( RandomIt first, RandomIt last, Compare comp );
  • first, last - the range of elements to sort
  • comp - comparison function object which returns ​true if the first argument is less than the second.

sort(RandomIt first, RandomIt last)

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

int main()
{
	int a[10] = {9,5,4,3,2,1,6,7,10,8}; 
	sort(a, a+10);
	 				
	for(int i = 0; i <10; ++i)
	{
		cout << a[i] << " ";
	}
}
  • sort(a, a+10);
    • a : start element of the data (the name of the array indicates the first data of the array)
    • a+10 : last element of the data (+10 means 10 is added from the start array address)

sort(RandomIt first, RandomIt last, Compare comp)

The powerful point of the sort() function is that we can set the criteria we want to sort by ourselves
Create comp() function and input as the third argument value of sort () function
Sort the datas according to comp() function

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

bool compare(int a, int b)
{
	return a > b;
}
int main()
{
	int a[10] = {9,5,4,3,2,1,6,7,10,8}; 
	sort(a, a+10, compare); 
	 				
	for(int i = 0; i <10; ++i)
	{
		cout << a[i] << " ";
	}
}

Sort the Object

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

class Student
{
public:
	string name;
	int score;
	Student(string name, int score)  //Constructor to initilize
	{
		this->name = name;
		this->score = score;
	}

	bool operator < (Student &student) 
	{
		return this->score < student.score;
	}
	 
};
int main()
{
	Student students[] = {
		Student("Monica", 97),
		Student("Rachel", 89),
		Student("Pheebs", 92)
	};
	sort(students, students+3);
	for(int i = 0; i <3; i++)
	{
		cout << students[i].name << " ";
		cout << students[i].score <<endl;
	}
}
  • bool operator < (Student &student)
    • When sort by using operator overloading, “<” operator will be used, so redefine if necessary
  • return this->score < student.score;
    • Return true if my score this.score is low when comparing with other student

Sort the data in Vector using pair

Use library Vector and Pair
Vector: Vector library in the form of a linked list

(1) single pair

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

int main()
{
	vector<pair<int, string>> v;
	v.push_back(pair<int, string>(90,"Alex")); 
	v.push_back(pair<int, string>(85,"Tom"));
	v.push_back(pair<int, string>(82,"Bread"));
	v.push_back(pair<int, string>(98,"Jonny"));
	v.push_back(pair<int, string>(79,"Luke"));
	
	sort(v.begin(), v.end());
	 
	for(int i = 0; i < v.size(); ++i)
	{
		cout << v[i].second << " ";
		cout << v[i].first <<endl; 
	}
}
  • push_back : inser the data in the end of the list
  • v.size(): return the size of the vector
  • Second of the currently declared pair means name information
  • pair
    • std::pair is a struct template that provides a way to store two heterogeneous objects as a single unit.
    • A pair is a specific case of a std::tuple with two elements.
    • The pair can have first and second values after it was declared.

(2) double pair

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

bool compare(pair<string, pair<int, int>> a,
			pair<string, pair<int, int>> b)
{
	if(a.second.first == b.second.first)
		return a.second.second > b.second.second;
	else
		return a.second.first > b.second.first;
}
int main()
{
	vector<pair<string, pair<int, int>>> v;
	v.push_back(pair<string, pair<int, int>>("Alex", pair<int, int>(90, 19960504))); 
	v.push_back(pair<string, pair<int, int>>("Dave", pair<int, int>(87, 19940218)));
	v.push_back(pair<string, pair<int, int>>("Andy", pair<int, int>(74, 19871214)));
	v.push_back(pair<string, pair<int, int>>("Luke", pair<int, int>(98, 19980123)));
	v.push_back(pair<string, pair<int, int>>("Clair", pair<int, int>(90, 19820612)));

	sort(v.begin(), v.end(), compare);
	 
	for(int i = 0; i < v.size(); ++i)
	{
		cout << v[i].first << " ";
		cout << v[i].second.first <<endl;
	}
}

  • Main graph Search algorithm
    BFS (Breadth First Search): Search for brother nodes at the same level of the vertex first
    DFS (Depth First Search): search for the children of vertex first

DFS Algorithm

  • Utilize Queue and Stack
  • Note that the BFS data structure uses two queues, while DFS uses a stack and a queue.

Time Complexity

  • DFS Time Complexity
    • Number of Vertexes: V
    • Number of Edges: E
    • Time Complexity: O(V + E)

C++

#include <iostream>
#include <cstdio> 
#include <vector>

using namespace std;

int visited[13];
vector<int> graph[13];
 
void dfs(int start)
{
	if(visited[start])
		return;
	visited[start] = true;
	cout << start << " ";
	
	for(int i = 0; i < graph[start].size(); ++i)
	{
		int next_index = graph[start][i];
		dfs(next_index);
	}
}

int main()
{
	graph[1].push_back(2);
	graph[2].push_back(1);
	
	graph[2].push_back(3);
	graph[3].push_back(2);
	
	graph[3].push_back(4);
	graph[4].push_back(3);
	
	graph[3].push_back(5);
	graph[5].push_back(3);

	graph[2].push_back(6);
	graph[6].push_back(2);
	
	graph[1].push_back(7);
	graph[7].push_back(1);
	
	graph[1].push_back(8);
	graph[8].push_back(1);
	
	graph[8].push_back(9);
	graph[9].push_back(8);

	graph[9].push_back(10);
	graph[10].push_back(9);	
		
	graph[9].push_back(11);
	graph[11].push_back(9);
	
	graph[8].push_back(12);
	graph[12].push_back(8);
	
	dfs(1);
	
	return 0;
}
  • Recursive call works as Stack

  • Main graph Search algorithm
    BFS (Breadth First Search): Search for brother nodes at the same level of the vertex first
    DFS (Depth First Search): search for the children of vertex first

BFS Algorithm

  • Utilize Queue
    • Create two queues: need_visit, visited

Time Complexity

  • BFS Time Complexity
    • Number of Vertexes: V
    • Number of Edges: E
    • Time Complexity: O(V + E)

C++

#include<iostream>
#include<vector> //to store data
#include<queue>

using namespace std;


int visited[13];
vector<int> graph[13];
 
void bfs(int start)
{
	queue<int> need_visit;
	
	need_visit.push(start);
	visited[start] = true;
	
	while(!need_visit.empty())
	{
		int x = need_visit.front();
		cout << x << " ";
		need_visit.pop();
		
		for(int i = 0; i < graph[x].size() ; ++i)
		{
			int y = graph[x][i];
			if(!visited[y])
			{
				need_visit.push(y);
				visited[y] = true;
			}
		}
	}
}
int main()
{
	graph[1].push_back(2);
	graph[2].push_back(1);
	
	graph[1].push_back(3);
	graph[3].push_back(1);
	
	graph[1].push_back(4);
	graph[4].push_back(1);
	
	graph[2].push_back(5);
	graph[5].push_back(2);

	graph[2].push_back(6);
	graph[6].push_back(2);
	
	graph[4].push_back(7);
	graph[7].push_back(4);
	
	graph[4].push_back(8);
	graph[8].push_back(4);
	
	graph[5].push_back(9);
	graph[9].push_back(5);

	graph[5].push_back(10);
	graph[10].push_back(5);	
		
	graph[7].push_back(11);
	graph[11].push_back(7);
	
	graph[7].push_back(12);
	graph[12].push_back(7);	
	
	bfs(1);
	
	return 0;
}

int visited[13];
- all elements are initialized with 0
vector<int> graph[13];
- index starts from 1