Skip to content

Commit

Permalink
v0 - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
filipemsilv4 committed Mar 6, 2023
1 parent 42c341a commit 11cfadd
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Lab1/DynamicAllocated/dynamicBubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// BubbleSort with doubly linked list

#include <iostream>
#include "list.cpp"

using namespace std;

int main(void){
LinkedList *list = new LinkedList();
// 70 1 12 8 99 72 5 15 20 91 14 61 66 41 81 88 16 21 34 90
list->add(70);
list->add(45);
list->add(24);
list->add(35);
list->add(5);

list->print();

//list->bubbleSort();
list->recursiveFasterBubbleSort(list->head, list->head->next, list->getSize());
}
113 changes: 113 additions & 0 deletions Lab1/DynamicAllocated/list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <iostream>
#include <cstdlib>

using namespace std;

class Node{
public:
int val;
Node* next;
Node* prev;
};

class LinkedList{
public:
Node* head;
Node* tail;
int size;

LinkedList(){
head = NULL;
tail = NULL;
size = 0;
}

void add(int val){
Node* temp = new Node;
temp->val = val;
temp->next = NULL;
temp->prev = NULL;
if(head == NULL){
head = temp;
tail = temp;
}
else{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
size++;
}

void print(){
Node* temp = head;
while(temp != NULL){
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}

int getSize(){ return size; }

void swap(Node *node1, Node *node2){
int temp = node1->val;
node1->val = node2->val;
node2->val = temp;
}

void bubbleSort(){
printf("size: %d\n", getSize());
Node *auxPrimary = head;
Node *auxSecondary = head->next;
int size = getSize();

for (int i = 0; i < size - 1; i++){
auxPrimary = head;
auxSecondary = head->next;
for (int j = 0; j < size - i - 1; j++){
if (auxPrimary->val > auxSecondary->val){
swap(auxPrimary, auxSecondary);
}
auxPrimary = auxSecondary;
auxSecondary = auxSecondary->next;
}
}
}

// Recursive Faster Bubble Sort
void recursiveFasterBubbleSort(Node *auxPrimary, Node *auxSecondary, int pos){
printf("------------------------\n");
// if the array is empty or has only one element, it is sorted
if (pos == 1){ return; }

// we will skip the already sorted elements, reducing the size of the array
int newSize = pos;

// if no swap is made, the array is sorted
bool swapped = false;

for (int j = 0; j < newSize - 1; j++){
if (auxPrimary->val > auxSecondary->val){
swap(auxPrimary, auxSecondary);
swapped = true;
print();
}
auxPrimary = auxSecondary;
auxSecondary = auxSecondary->next;
}

// if no swap is made, the array is sorted
if (!swapped){ return; }

// the last element is sorted, so we can skip it
newSize--;

// reset the pointers
auxPrimary = head;
auxSecondary = head->next;

// sort the remaining array
recursiveFasterBubbleSort(auxPrimary, auxSecondary, newSize);
}
};
147 changes: 147 additions & 0 deletions Lab1/StaticAllocated/staticBubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <iostream>
using namespace std;


void bubbleSort(int [], int);
void fasterBubbleSort(int [], int);
void displayArray(int [], int);
void generateRandomArray(int [], int);
void recursiveFasterBubbleSort(int [], int);


int main(int argc, char *argv[]){
// Ensure that the user has entered a VALID command line argument
if (argc != 2 || (argv[1][0] != 'f' && argv[1][0] != 'r' && argv[1][0] != 'n')){
cout << "Usage: sort f|n|r" << endl;
cout << "f = faster bubble sort" << endl;
cout << "n = normal bubble sort" << endl;
cout << "r = recursive faster bubble sort" << endl;
return 1;
}

int values[] = { 5, 7, 2, 8, 9, 1, 4, 3, 6 };
int size = sizeof(values) / sizeof(int);

cout << "The unsorted values are " << endl;

displayArray(values, size);

if (argc > 1 && argv[1][0] == 'f'){
for (int i = 0; i < 100000; i++){
// Generate a random array each iteration
generateRandomArray(values, size);
fasterBubbleSort(values, size);
cout << "The sorted values are " << endl;
displayArray(values, size);
}
} else if (argc > 1 && argv[1][0] == 'r'){
for (int i = 0; i < 100000; i++){
// Generate a random array each iteration
generateRandomArray(values, size);
recursiveFasterBubbleSort(values, size);

cout << "The sorted values are " << endl;
displayArray(values, size);
}
} else if (argc > 1 && argv[1][0] == 'n'){
for (int i = 0; i < 100000; i++){
// Generate a random array each iteration
generateRandomArray(values, size);
bubbleSort(values, size);

cout << "The sorted values are " << endl;
displayArray(values, size);
}
}

// print argc and argv[1][0]
cout << "argc = " << argc << endl;
cout << "argv[1][0] = " << argv[1][0] << endl;

return 0;
}


// Generate a random array
void generateRandomArray(int array[], int size){
for (int count = 0; count < size; count++)
array[count] = rand() % 100;
}


// Display the array
void displayArray(int array[], int size){
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}


// Bubble sort
void bubbleSort(int array[], int size){
for (int i = 0; i < size - 1; i++) // Last i elements are already in place
for (int j = 0; j < size - 1; j++) // Move the largest element to the end
if (array[j] > array[j + 1]){
swap(array[j], array[j + 1]);
/* cout << "The array is now ";
displayArray(array, size); */
}
}


// Faster Bubble Sort
void fasterBubbleSort(int array[], int size){
// we will skip the already sorted elements, reducing the size of the array
int newSize = size;

for (int i = 0; i < size - 1; i++){
// if no swap is made, the array is sorted
bool swapped = false;

for (int j = 0; j < newSize - 1; j++)
if (array[j] > array[j + 1]){
swap(array[j], array[j + 1]);
swapped = true;
}

// if no swap is made, the array is sorted
if (!swapped)
break;

// the last element is sorted, so we can skip it
newSize--;
}
}


// Recursive Faster Bubble Sort
void recursiveFasterBubbleSort(int array[], int size){
// if the array is empty or has only one element, it is sorted
if (size == 1){ return; }

// we will skip the already sorted elements, reducing the size of the array
int newSize = size;

// if no swap is made, the array is sorted
bool swapped = false;

for (int j = 0; j < newSize - 1; j++){
if (array[j] > array[j + 1]){
swap(array[j], array[j + 1]);
swapped = true;
}
}

// if no swap is made, the array is sorted
if (!swapped)
return;

// the last element is sorted, so we can skip it
newSize--;

// sort the remaining array
recursiveFasterBubbleSort(array, newSize);
}


// I hope it works
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# IFNMG-Busca_e_Ordenacao-2023-1
# IFNMG-Busca_e_Ordenacao-2023-1
- Lab1: Bubble Sort implementation on both dynamic and static allocated lists

0 comments on commit 11cfadd

Please sign in to comment.