Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Profiler.h"
- #include "Profiler.h"
- #define AVG_BUBBLE_COMP "avg_bubble_comp"
- #define AVG_BUBBLE_ASSIG "avg_bubble_assig"
- #define AVG_BUBBLE_OP "avg_bubble_operations"
- #define AVG_INSERTION_COMP "avg_insertion_comp"
- #define AVG_INSERTION_ASSIG "avg_insertion_assig"
- #define AVG_INSERTION_OP "avg_insertion_operations"
- #define AVG_SELECTION_COMP "avg_selection_comp"
- #define AVG_SELECTION_ASSIG "avg_selection_assig"
- #define AVG_SELECTION_OP "avg_selection_operations"
- #define BEST_BUBBLE_COMP "best_bubble_comp"
- #define BEST_BUBBLE_ASSIG "best_bubble_assig"
- #define BEST_BUBBLE_OP "best_bubble_op"
- #define BEST_INSERTION_COMP "best_insertion_comp"
- #define BEST_INSERTION_ASSIG "best_insertion_assig"
- #define BEST_INSERTION_OP "best_insertion_op"
- #define BEST_SELECTION_COMP "best_selection_comp"
- #define BEST_SELECTION_ASSIG "best_selection_assig"
- #define BEST_SELECTION_OP "best_selection_op"
- #define WORST_BUBBLE_COMP "worst_bubble_comp"
- #define WORST_BUBBLE_ASSIG "worst_bubble_assig"
- #define WORST_BUBBLE_OP "worst_bubble_op"
- #define WORST_SELECTION_COMP "worst_selection_comp"
- #define WORST_SELECTION_ASSIG "worst_selection_assig"
- #define WORST_SELECTION_OP "worst_selection_op"
- #define WORST_INSERTION_COMP "worst_insertion_comp"
- #define WORST_INSERTION_ASSIG "worst_insertion_assig"
- #define WORST_INSERTION_OP "worst_insertion_op"
- using namespace std;
- Profiler profiler("sort");
- int comp_bubble, comp_insert, comp_sel, assig_bubble, assig_insert, assig_sel, op_bubble, op_insert, op_sel;
- void printArray(int* a, int n)
- {
- for (int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << "\n";
- }
- void bubbleSort(int* a, int n)
- {
- assig_bubble = 0;
- comp_bubble = 0;
- int i, j, aux;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < n - i - 1; j++)
- {
- if (a[j] > a[j + 1])
- {
- aux = a[j];
- a[j] = a[j + 1];
- a[j + 1] = aux;
- assig_bubble += 3;
- }
- comp_bubble++;
- }
- //printArray(a, 5);
- }
- //op_bubble = comp_bubble + assig_bubble;
- }
- void selectionSort(int* a, int n)
- {
- assig_sel = 0;
- comp_sel = 0;
- int buff, aux;
- for (int i = 0; i < n - 1; i++)
- {
- buff = i + 1;
- for (int j = i + 2; j < n; j++)
- {
- if (a[buff] > a[j]) buff = j;
- comp_sel++;
- }
- if (a[i] < a[buff])
- {
- aux = a[i + 1];
- a[i + 1] = a[buff];
- a[buff] = aux;
- assig_sel += 3;
- }
- else
- {
- aux = a[i];
- a[i] = a[buff];
- a[buff] = aux;
- assig_sel += 3;
- }
- comp_sel++;
- }
- }
- void insertionSort(int* a, int n)
- {
- assig_insert = 0;
- comp_insert = 0;
- int buff, j;
- for (int i = 1; i < n; i++)
- {
- buff = a[i];
- assig_insert++;
- j = i - 1;
- while (j >= 0 && buff < a[j])
- {
- a[j + 1] = a[j];
- assig_insert++;
- j--;
- }
- comp_insert++;
- a[j + 1] = buff;
- assig_insert++;
- }
- }
- void demoBubble()
- {
- cout << "Demo Bubble" << "\n";
- int a[5] = { 9,3,7,11,12 };
- bubbleSort(a, 5);
- printArray(a, 5);
- }
- void demoSelection()
- {
- cout << "\n" << "Demo Selection" << "\n";
- int a[5] = { 9,3,7,11,12 };
- selectionSort(a, 5);
- printArray(a, 5);
- }
- void demoInsertion()
- {
- int n = 5;
- cout << "\n" << "Demo Insertion" << "\n";
- int a[5] = { 9,3,7,11,12 };
- insertionSort(a, n);
- printArray(a, n);
- }
- int* generateAvgCase(int n)
- {
- int* a = (int*)malloc(n * sizeof(int));
- FillRandomArray(a, n);
- return a;
- }
- int* generateAscendingOrder(int n)
- {
- int* a = (int*)malloc(n * sizeof(int));
- FillRandomArray(a, n, 10, 300, false, 1);
- return a;
- }
- int* generateDescendingOreder(int n)
- {
- int* a = (int*)malloc(n * sizeof(int));
- FillRandomArray(a, n, 10, 300, false, 2);
- return a;
- }
- void generateGraphs()
- {
- //avg case for bubble sort
- for (int size = 100; size <= 10000; size += 500)
- {
- int* a = generateAvgCase(size);
- bubbleSort(a, size);
- profiler.countOperation(AVG_BUBBLE_COMP, size, comp_bubble);
- profiler.countOperation(AVG_BUBBLE_ASSIG, size, assig_bubble);
- profiler.addSeries(AVG_BUBBLE_OP, AVG_BUBBLE_COMP, AVG_BUBBLE_ASSIG);
- free(a);
- a = generateAvgCase(size);
- selectionSort(a, size);
- profiler.countOperation(AVG_SELECTION_COMP, size, comp_sel);
- profiler.countOperation(AVG_SELECTION_ASSIG, size, assig_sel);
- profiler.addSeries(AVG_SELECTION_OP, AVG_SELECTION_COMP, AVG_SELECTION_ASSIG);
- free(a);
- a = generateAvgCase(size);
- insertionSort(a, size);
- profiler.countOperation(AVG_INSERTION_COMP, size, comp_insert);
- profiler.countOperation(AVG_INSERTION_ASSIG, size, assig_insert);
- profiler.addSeries(AVG_INSERTION_OP, AVG_INSERTION_COMP, AVG_INSERTION_ASSIG);
- free(a);
- profiler.createGroup("Average Case Op", AVG_BUBBLE_OP, AVG_SELECTION_OP, AVG_INSERTION_OP);
- profiler.createGroup("Average Case Assig", AVG_BUBBLE_ASSIG, AVG_SELECTION_ASSIG, AVG_INSERTION_ASSIG);
- profiler.createGroup("Average Case Comp", AVG_BUBBLE_COMP, AVG_SELECTION_COMP, AVG_INSERTION_COMP);
- ///best cases
- a = generateAscendingOrder(size);
- bubbleSort(a, size);
- profiler.countOperation(BEST_BUBBLE_COMP, size, comp_bubble);
- profiler.countOperation(BEST_BUBBLE_ASSIG, size, assig_bubble);
- profiler.addSeries(BEST_BUBBLE_OP, BEST_BUBBLE_COMP, BEST_BUBBLE_ASSIG);
- free(a);
- a = generateAscendingOrder(size);
- selectionSort(a, size);
- profiler.countOperation(BEST_BUBBLE_COMP, size, comp_sel);
- profiler.countOperation(BEST_BUBBLE_ASSIG, size, assig_sel);
- profiler.addSeries(BEST_SELECTION_OP, BEST_SELECTION_COMP, BEST_SELECTION_ASSIG);
- free(a);
- a = generateAscendingOrder(size);
- insertionSort(a, size);
- profiler.countOperation(BEST_INSERTION_COMP, size, comp_insert);
- profiler.countOperation(BEST_INSERTION_ASSIG, size, assig_insert);
- profiler.addSeries(BEST_INSERTION_OP, BEST_INSERTION_COMP, BEST_INSERTION_ASSIG);
- free(a);
- profiler.createGroup("Best Case Comp", BEST_BUBBLE_COMP, BEST_SELECTION_COMP, BEST_INSERTION_COMP);
- profiler.createGroup("Best Case Assig", BEST_BUBBLE_ASSIG, BEST_SELECTION_ASSIG, BEST_INSERTION_ASSIG);
- profiler.createGroup("Best Case Op", BEST_BUBBLE_OP, BEST_SELECTION_OP, BEST_INSERTION_OP);
- ///worst cases
- a = generateDescendingOreder(size);
- bubbleSort(a, size);
- profiler.countOperation(WORST_BUBBLE_COMP, size, comp_bubble);
- profiler.countOperation(WORST_BUBBLE_ASSIG, size, assig_bubble);
- profiler.addSeries(WORST_BUBBLE_OP, WORST_BUBBLE_COMP, WORST_BUBBLE_ASSIG);
- free(a);
- a = generateDescendingOreder(size);
- selectionSort(a, size);
- profiler.countOperation(WORST_SELECTION_COMP, size, comp_sel);
- profiler.countOperation(WORST_SELECTION_ASSIG, size, assig_sel);
- profiler.addSeries(WORST_SELECTION_OP, WORST_SELECTION_COMP, WORST_SELECTION_ASSIG);
- free(a);
- a = generateDescendingOreder(size);
- insertionSort(a, size);
- profiler.countOperation(WORST_INSERTION_COMP, size, comp_insert);
- profiler.countOperation(WORST_INSERTION_ASSIG, size, assig_insert);
- profiler.addSeries(WORST_INSERTION_OP, WORST_INSERTION_COMP, WORST_INSERTION_ASSIG);
- free(a);
- profiler.createGroup("Worst Case Comp", WORST_BUBBLE_COMP, WORST_SELECTION_COMP, WORST_INSERTION_COMP);
- profiler.createGroup("Worst Case Assig", WORST_BUBBLE_ASSIG, WORST_SELECTION_ASSIG, WORST_INSERTION_ASSIG);
- profiler.createGroup("Worst Case Op", WORST_BUBBLE_OP, WORST_SELECTION_OP, WORST_INSERTION_OP);
- }
- }
- int main()
- {
- //demoBubble();
- //demoInsertion();
- //demoSelection();
- generateGraphs();
- profiler.showReport();
- //printArray(a, 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement