๋ฐ˜๊ฐ€์›Œ์š”! ํ—ˆ๋ธŒ์ž…๋‹ˆ๋‹ค!

์ €๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ํ˜„๋Œ€ ์—ฐ๊ธˆ์ˆ ์‚ฌ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ๋Š๋‚€ ์ ๋“ค๊ณผ ์ด์•ผ๊ธฐ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

์ž๋ฃŒ๊ตฌ์กฐ

[์ž๋ฃŒ๊ตฌ์กฐ] c++ sortํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ์ •๋ ฌ

mmin.h 2020. 8. 12. 11:50

์•ˆ๋…•ํ•˜์„ธ์š” ์ฃผ์ธ์žฅ H์ž…๋‹ˆ๋‹ค. 

์˜ค๋Š˜์€ ๊ธฐ์กด์— ๋ฐฐ์šด ์ •๋ ฌ๋ณด๋‹ค ์ข€๋” ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. 

 

sortํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ quicksort์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ง„ํ–‰ ๋˜์ง€๋งŒ ์ฐจ์ด์ ์œผ๋กœ 

O(log N * N)๊ฐ€ ๋ณด์žฅ๋œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ฃ  

 

๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

#include <iostream>
#include <algorithm>

using namespace std;

class Student {
public:
	string name; 
	int score; 
	Student(string name, int score) {
		this->name = name; 
		this->score = score; 
	}
	//์ •๋ ฌ ๊ธฐ์ค€์€ '์ ์ˆ˜๊ฐ€ ์ž‘์€ ์ˆœ์„œ' 
	bool operator < (Student& student) {
		return this->score < student.score;
	}
};

//bool compare(int a, int b) {
//	return a > b; 
//}

int main(void) {
	Student students[] = {
		Student("kim",90),
		Student("choi",93),
		Student("pack",97),
		Student("heo",87),
		Student("lee",92),
	};

	sort(students, students + 5);
	for (int i = 0; i < 5; i++) {
		cout << students[i].name << ' ';
	}
}

 ์ฃผ์„์ฒ˜๋ฆฌ๋œ compare ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์ง€๊ธˆ์€ class ๋‚ด ๋น„๊ต๊ตฐ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

ํ•˜์ง€๋งŒ class๋Š” ์‹ค์ œ ์‹ค๋ฌด์—์„œ ๋งŽ์ด ์“ฐ์ด๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋Œ€ํšŒ์—์„  shotcoding ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง„ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std; 

//๋ฐฑํ„ฐ ๋ผ์ด๋ฒ„๋ฆฌ๋ฆฌ์™€ ํŽ˜์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ์ˆ์ฝ”๋”ฉ
int main(void) {
	vector<pair<int, string>> v; 
	v.push_back(pair<int, string>(90,"park"));
	v.push_back(pair<int, string>(85, "lee"));
	v.push_back(pair<int, string>(82, "na"));
	v.push_back(pair<int, string>(87, "kang"));
	v.push_back(pair<int, string>(79, "choi"));

	sort(v.begin(), v.end());
	for (int i = 0; i < v.size(); i++) {
		cout << v[i].second << ' ';
	}
	return 0; 
}

์ด๋ ‡๊ฒŒ ๋ฒกํ„ฐ์™€ ํŽ˜์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ํ›จ์‹  ๋น ๋ฅด๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•ด๋ณด๋Š” ์ฝ”๋“œ๋ฅผ ์ ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

//ํ•™์ƒ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๊ฐ€ ์ด๋ฆ„, ์„ฑ์ ,์ƒ๋…„์›”์ผ์ผ ๋•Œ
//ํ•™์ƒ์„ ์„ฑ์  ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•˜๊ณ ์ž ํ•œ๋‹ค. 
//๋‹ค๋งŒ ์„ฑ์ ์ด ๋™์ผํ•œ ๊ฒฝ์šฐ ๋‚˜์ด๊ฐ€ ๋” ์–ด๋ฆฐ ํ•™์ƒ์ด ๋” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค. 

//๋จผ์ € ์„ฑ์ ์— ๋”ฐ๋ผ ์ •๋ ฌ ํ›„, ์„ฑ์ ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋‚˜์ด๊ฐ€ ๋” ์–ด๋ฆฐ ์‚ฌ๋žŒ

#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(void) {
	vector<pair<string, pair<int, int>>> v;
	v.push_back(pair<string, pair<int, int>>("๋‚˜๋™๋นˆ", make_pair(90, 19961212)));
	v.push_back(pair<string, pair<int, int>>("์ดํƒœ์ผ", make_pair(97, 19930518)));
	v.push_back(pair<string, pair<int, int>>("๋ฐ•ํ•œ์šธ", make_pair(95, 19930203)));
	v.push_back(pair<string, pair<int, int>>("์ด์ƒ์šฑ", make_pair(90, 19921207)));
	v.push_back(pair<string, pair<int, int>>("๊ฐ•์ข…๊ตฌ", make_pair(88, 19900302)));

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

 

ํ•ด๋‹น ์˜ˆ์ œ๋Š” ์•„๋ž˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋”ฐ๋ผ ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค. 

https://blog.naver.com/ndb796/221227975229

 

8. C++ STL sort() ํ•จ์ˆ˜ ๋‹ค๋ฃจ๊ธฐ โ‘ 

์ง€๋‚œ ์‹œ๊ฐ„๊นŒ์ง€ ์„ ํƒ ์ •๋ ฌ, ๋ฒ„๋ธ” ์ •๋ ฌ, ์‚ฝ์ž… ์ •๋ ฌ, ํ€ต ์ •๋ ฌ, ๋ณ‘ํ•ฉ ์ •๋ ฌ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ  ๊ฐ„๋‹จํ•œ ํ”„๋กœ...

blog.naver.com