Advertisement
CHU2

Insertion Sort

Mar 20th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>       //seems like I can't get the <getline> function to work without initializing <string> header
  4.  
  5. using namespace std;
  6.  
  7. struct product {
  8.     int ID;
  9.     string name;
  10.     float price;
  11. };
  12.  
  13. void insertionSort(product array[], const int);
  14. void printArray(product array[], const int);
  15. void textfilein(product array[], const int);
  16. void textfileout();
  17.  
  18. int main() {
  19.     const int size = 5;
  20.     product p[size] = {{105, "Once again, I made something worthless, by Goemon", 5437.85}, {193, "Time Machine (Prototype)", 2212.12}, {102, "Bamboo Helicam", 176.19}, {106, "Cyalume Saber", 0.20}, {104, "Moad Snake", 4078.39}};
  21.  
  22.     cout << "--------------------------------------------------------------------------------------------------\n";
  23.     cout << "The products are: \n\n";
  24.     textfilein(p, size);       //text file input then output
  25.     textfileout();
  26.    
  27.     cout << "--------------------------------------------------------------------------------------------------\n";
  28.     cout << "The product after sort are: \n\n";
  29.     insertionSort(p, size);     //do insertion sort then text file input then output
  30.     textfilein(p, size);
  31.     textfileout();
  32.  
  33.     return 0;
  34. }
  35.  
  36. void insertionSort(product arr[], const int n)      //insertion sort, a copypasta from kan Gabumpa report
  37. {
  38.     for (int i = 1; i < n; i++)
  39.     {
  40.         int key = arr[i].ID;        //initialize <ID> since amo la an ig cocompare na value dire na an iba na aada <struct>
  41.         product key2 = arr[i];      //initialize another key but this is only for a <key> to hold value to swap later. swapping doesn't work without this
  42.         int j = i - 1;
  43.  
  44.         // Move elements of arr[0..i-1],        //a copypasta comment, just leaves it there for quality purposes
  45.         // that are greater than key, to one
  46.         // position ahead of their
  47.         // current position
  48.         while (j >= 0 && arr[j].ID > key)
  49.         {
  50.             arr[j + 1] = arr[j];
  51.             j = j - 1;
  52.         }
  53.         arr[j + 1] = key2;
  54.     }
  55. }
  56.  
  57. void printArray(product arr[], const int n)     //did use this to try if the insertion sort works but ignores after implementing text files databases
  58. {
  59.     for (int i = 0; i < n; i++) {      
  60.         cout << "ID: " << arr[i].ID << "\n";
  61.         cout << "Name: " << arr[i].name << "\n";
  62.         cout << "Price: " << arr[i].price << " PHP \n\n";
  63.     }
  64. }
  65.  
  66. void textfilein(product arr[], const int n) {       //for text file to record the values
  67.     fstream tf;
  68.  
  69.     tf.open("LAF2textfile.txt");
  70.     for (int i = 0; i < n; i++) {
  71.         tf << "ID: " << arr[i].ID << "\n";
  72.         tf << "Name: " << arr[i].name << "\n";
  73.         tf << "Price: " << arr[i].price << " PHP \n\n";
  74.     }
  75.     tf.close();
  76. }
  77.  
  78. void textfileout() {        //outputs the values/lines in the text file
  79.     fstream tf;
  80.     string line;
  81.  
  82.     tf.open("LAF2textfile.txt");
  83.     while (getline(tf, line)) {
  84.         cout << line << "\n";
  85.     }
  86.     tf.close();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement