Advertisement
Guest User

C++ MGE top-down merge sort

a guest
Oct 1st, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. int input_cmp(std::string a, std::string b) {
  8.     char choice;
  9.     cout << a << " < " << b << " (y/n): ";
  10.     cin >> choice;
  11.     if (tolower(choice) == 'y') return -1;
  12.     return 0;
  13. }
  14.  
  15. #define SORT_TYPE std::string
  16. #define SORT_CMP input_cmp
  17. #define MG_SIZE 212
  18.  
  19. void copy_array(SORT_TYPE *a, SORT_TYPE *b, int n) {
  20.     for (int i = 0; i < n; i++) {
  21.         a[i] = b[i];
  22.     }
  23. }
  24.  
  25. void merge(SORT_TYPE *a, int left, int right, int end, SORT_TYPE *b) {
  26.     int i = left, j = right;
  27.     for (int k = left; k < end; k++) {
  28.         if (i < right && (j >= end || SORT_CMP(a[i], a[j]) < 0)) {
  29.             b[k] = a[i];
  30.             i++;
  31.         } else {
  32.             b[k] = a[j];
  33.             j++;
  34.         }
  35.     }
  36. }
  37.  
  38. void merge_sort(SORT_TYPE *a, int begin, int end, SORT_TYPE *b) {
  39.     if (end - begin < 2) return;
  40.     int mid = (begin + end) / 2;
  41.     merge_sort(b, begin, mid, a);
  42.     merge_sort(b, mid, end, a);
  43.     merge(b, begin, mid, end, a);
  44. }
  45.  
  46. int main() {
  47.     std::string girls[MG_SIZE] = {"Akaname","Alice","Alp","Alraune","Amazoness","Angel","Ant Arachne","Anubis","Apophis","Apsara","Arachne","Arch Imp","Atlach-Nacha","Automaton","Baphomet","Barometz","Basilisk","Beelzebub","Bicorn","Black Harpy","Blue Oni","Bubble Slime","Bunyip","Cait Sith","Cancer","Centaur","Charybdis","Cheshire Cat","Chimaera","Chochin-Obake","Cockatrice","Crow Tengu","Cu Sith","Cupid","Cursed Sword","Cyclops","Dark Angel","Dark Elf","Dark Mage","Dark Matter","Dark Priest","Dark Slime","Dark Valkyrie","Demon","Devil","Devil Bug","Dhampir","Doppelganger","Dormouse","Dorome","Dragon","Dragon Zombie","Dryad","Dullahan","Dwarf","Echidna","Elf","Fairy","Familiar","Flow Kelp","Gandharva","Gargoyle","Gazer","Ghost","Ghoul","Giant Ant","Giant Slug","Girtablilu","Glacies","Gnome","Goblin","Golem","Greenworm","Gremlin","Griffon","Grizzly","Gyoubu Danuki","Hakutaku","Harpy","Hellhound","High Orc","Hinezumi","Hobgoblin","Holstaur","Honey Bee","Hornet","Houri","Humpty Egg","Ice Queen","Ignis","Imp","Inari","Ittan-momen","Jabberwock","Jiangshi","Jinko","Jinn of the Jar","Jorou-Gumo","Jubjub","Kakuen","Kamaitachi","Kappa","Karakasa-Obake","Kejourou","Kesaran Pasaran","Khepri","Kikimora","Kitsune-bi","Kitsune-tsuki","Kobold","Kraken","Kunoichi","Lamia","Large Mouse","Lava Golem","Leanan Sidhe","Lesser Succubus","Lich","Lilim","Liliraune","Living Armor","Living Doll","Lizardman","Mad Hatter","Mandragora","Manticore","Mantis","March Hare","Matango","Medusa","Mermaid","Merrow","Mershark","Mimic","Mindflayer","Minotaur","Mothman","Mucus Toad","Mummy","Nekomata","Nereid","Night Gaunt","Nightmare","Nurarihyon","Nureonago","Ochimusha","Ogre","Oomukade","Orc","Otohime","Owl Mage","Papillon","Parasite Slime / Slime Carrier","Phantom","Pharaoh","Pixie","Queen Slime","Raiju","Ratatoskr","Red Oni","Red Slime","Redcap","Ren Xiongmao","Roper","Ryu","Sahuagin","Salamander","Sandworm","Satyros","Scylla","Sea Bishop","Sea Slime","Selkie","Shirohebi","Shoggoth","Siren","Skeleton","Slime","Soldier Beetle","Sphinx","Succubus","Sylph","Tentacle","Thunderbird","Titania","Tritonia","Troll","Trumpart","Umi Osho","Unagi Joro","Undine","Unicorn","Ushi-Oni","Valkyrie","Vamp Mosquito","Vampire","Wendigo","Werebat","Werecat","Wererabbit","Weresheep","Werewolf","White Horn","Wight","Will-o-the-Wisp","Witch","Wurm","Wyvern","Yeti","Youko","Yuki-Onna","Zombie"};
  48.    
  49.     reverse(begin(girls), end(girls));
  50.     SORT_TYPE tmp[MG_SIZE];
  51.     copy_array(tmp, girls, MG_SIZE);
  52.     merge_sort(girls, 0, MG_SIZE, tmp);
  53.    
  54.     fstream f;
  55.     f.open("sorted monster girls.txt", fstream::out);
  56.     if (!f.is_open()) {
  57.         cerr << "failed to open output file\n";
  58.         exit(1);
  59.     }
  60.     cout << "\n";
  61.     for (int i = 0; i < MG_SIZE; i++) {
  62.         f << girls[i] << "\n";
  63.         cout << girls[i] << "\n";
  64.     }
  65.     f.close();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement