-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting.cpp
41 lines (31 loc) · 1.18 KB
/
sorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Copyright (c) 2012 Cheese and Bacon Games, LLC */
/* This file is licensed under the MIT License. */
/* See the file docs/LICENSE.txt for the full license text. */
#include "sorting.h"
#include <cmath>
using namespace std;
vector<GUI_Object> quick_sort(vector<GUI_Object> objects,bool sort_by_y){
if(objects.size()<=1){
return objects;
}
vector<GUI_Object> less_than;
vector<GUI_Object> greater_than;
vector<GUI_Object> result;
int pivot=(int)floor((double)(objects.size()-1)/2.0);
for(int i=0;i<objects.size();i++){
if(i!=pivot){
if((sort_by_y && objects[i].y<=objects[pivot].y) || (!sort_by_y && objects[i].x<=objects[pivot].x)){
less_than.push_back(objects[i]);
}
else{
greater_than.push_back(objects[i]);
}
}
}
vector<GUI_Object> sorted_less=quick_sort(less_than,sort_by_y);
vector<GUI_Object> sorted_greater=quick_sort(greater_than,sort_by_y);
result.insert(result.end(),sorted_less.begin(),sorted_less.end());
result.push_back(objects[pivot]);
result.insert(result.end(),sorted_greater.begin(),sorted_greater.end());
return result;
}