Skip to content
Snippets Groups Projects
Commit 304d06f4 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Added sorts insertion and bubble

parent 94adbc8d
No related branches found
No related tags found
No related merge requests found
{
"files.associations": {
"jrandom.h": "c"
}
}
\ No newline at end of file
CC := gcc
CC_FLAGS := -Wall -Wextra
CC_FLAGS := -Wall -Wextra -fsanitize=address -g
BIN := bin
SRC := src
......
No preview for this file type
......@@ -5,12 +5,18 @@
! DESCRIPTION
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#ifndef _JRANDOM_H_
#define _JRANDOM_H_
// DEFINES
// PROTOTYPE FUNCTIONS
uint64_t getRandomNumber(uint64_t highLimit, uint64_t lowLimit);
uint64_t getRandomNumber(uint64_t lowLimit, uint64_t highLimit);
#endif
\ No newline at end of file
......@@ -5,21 +5,43 @@
! DESCRIPTION
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#ifndef _JTABLEAUX_H_
#define _JTABLEAUX_H_
// DEFINES
// PROTOTYPE FUNCTIONS
void showTab(uint64_t *tab, uint64_t tabSize);
void fillTabRandom(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max);
uint64_t* createTab(uint64_t tabSize);
void destroyTab(uint64_t * tab);
uint64_t* sumTabs(uint64_t* tab1, uint64_t* tab2, uint64_t tabSize);
uint64_t* mulTabs(uint64_t* tab1, uint64_t multiplier, uint64_t tabSize);
double* convertToDouble(uint64_t* tab, uint64_t tabSize);
void showTab(uint64_t *tab, uint64_t tabSize, char* text);
void fillTabRandomValues(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max);
void fillTabWithZeroes(uint64_t *tab, uint64_t tabSize);
void fillTabUniformRandom(uint64_t* tab, uint64_t tabSize);
uint64_t getTabSmallestValue(uint64_t *tab, uint64_t tabSize);
uint64_t getTabBiggestValue(uint64_t *tab, uint64_t tabSize);
void shuffleTab(uint64_t* tab, uint64_t tabSize, uint64_t numberOfSwaps);
void losslessShifTab(uint64_t* tab, uint64_t tabSize, uint64_t n);
uint64_t getTabSmallestValueIndex(uint64_t *tab, uint64_t tabSize);
uint64_t getTabBiggestValueIndex(uint64_t *tab, uint64_t tabSize);
uint64_t getIndexOfTabElement(uint64_t *tab, uint64_t tabSize, uint64_t element);
uint64_t getNumberOfSmallerElements(uint64_t* tab, uint64_t tabSize, uint64_t valElement);
void sortTabLTH(uint64_t *tab, uint64_t tabSize);
void sortInsertionLTH(uint64_t *tab, uint64_t tabSize);
void sortInsertionHTL(uint64_t *tab, uint64_t tabSize);
void countOccurence(uint64_t *tab, uint64_t tabSize, uint64_t *occurences, uint64_t sizeOfTabOcc);
double getMeanOfTab(uint64_t *tab, uint64_t tabSize);
......@@ -29,4 +51,6 @@ uint64_t getMedianOfTab(uint64_t *tab, uint64_t tabSize);
void showVerticalHistoOfTab(uint64_t *tab, uint64_t tabSize);
void showHorizonalHistoOfTab(uint64_t *occurences, uint64_t tabSize);
void swapIndex(uint64_t* tab, uint64_t index1, uint64_t index2);
#endif
\ No newline at end of file
/*
* HEADER SORT
* Author : Jonas S.
* Date : 10/11/2021
! DESCRIPTION
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef _SORT_H_
#define _SORT_H_
// DEFINES
// PROTOTYPE FUNCTIONS
void quicksort(uint64_t* tab, uint64_t tabSize, uint64_t first, uint64_t last);
#endif
\ No newline at end of file
......@@ -6,6 +6,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
......@@ -14,11 +15,11 @@
/**
* @brief Returns a random number
*
* @param highLimit
* @param lowLimit
* @param highLimit
* @return uint64_t
*/
uint64_t getRandomNumber(uint64_t highLimit, uint64_t lowLimit)
uint64_t getRandomNumber(uint64_t lowLimit, uint64_t highLimit)
{
return (rand() % (highLimit - lowLimit + 1) + lowLimit);
}
\ No newline at end of file
......@@ -2,26 +2,81 @@
* CODE TABLEAUX_UNI
* Author : Jonas S.
* Date : 12/10/2021
! Gestion de base de tableaux
! https://malaspinas.academy/prog_seq_c_tp/tableaux_unidimensionnels/index.html
! Gestion des tableaux avec malloc
! https://malaspinas.academy/prog_seq_c_tp/tableaux_unidimensionnels_dyn/tableaux_unidimensionnels_dyn.pdf
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "../include/jtableaux.h"
#include "../include/jrandom.h"
#include "../include/jtableaux.h"
/**
* @brief Swap the values between to indexes of an array
*
* @param tab
* @param index1
* @param index2
*/
void swapIndex(uint64_t* tab, uint64_t index1, uint64_t index2)
{
uint64_t tmp;
tmp = tab[index1];
tab[index1] = tab[index2];
tab[index2] = tmp;
}
/**
* @brief Swap random indexe's values
*
* @param tab
* @param tabSize
*/
void randomSwap(uint64_t* tab, uint64_t tabSize)
{
swapIndex(tab, getRandomNumber(0, tabSize-1), getRandomNumber(0, tabSize-1));
}
/**
* @brief Allocate memory for a tab of given size
* and returns a pointer to it
*
* @param tabSize
* @return uint64_t*
*/
uint64_t* createTab(uint64_t tabSize)
{
uint64_t* tab = malloc(tabSize * sizeof(uint64_t));
if(tab == NULL)
{
printf("MEMORY ALLOCATION FAILED\n");
exit(0);
}
return tab;
}
/**
* @brief Dealocate the memory of a given tab
*
* @param tab
*/
void destroyTab(uint64_t * tab)
{
free(tab);
}
/**
* @brief Print an array in the terminal
*
* @param tab
* @param tabSize
* @param text TAB NAME
*/
void showTab(uint64_t *tab, uint64_t tabSize)
void showTab(uint64_t *tab, uint64_t tabSize, char* text)
{
printf("%s : ", text);
for(uint64_t i = 0; i < tabSize; i++)
{
printf(" %ld,", tab[i]);
......@@ -37,14 +92,84 @@ void showTab(uint64_t *tab, uint64_t tabSize)
* @param min
* @param max
*/
void fillTabRandom(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max)
void fillTabRandomValues(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max)
{
for(uint64_t i = 0; i < tabSize; i++)
{
tab[i] = getRandomNumber(max, min);
tab[i] = getRandomNumber(min, max);
}
}
/**
* @brief Shuffle the values in a tab
*
* @param tab
* @param tabSize
* @param numberOfSwaps
*/
void shuffleTab(uint64_t* tab, uint64_t tabSize, uint64_t numberOfSwaps)
{
for(uint64_t i = 0; i < numberOfSwaps; i++)
{
randomSwap(tab, tabSize);
}
}
/**
* @brief Shift the tab pushing last element to first
* Shifting to the right
*
* @param tab
* @param tabSize
* @param n Number of shifts
*/
void losslessShifTab(uint64_t* tab, uint64_t tabSize, uint64_t n)
{
uint64_t lastValue;;
for(uint64_t j = 0; j < n; j++)
{
lastValue = tab[tabSize-1];
// Shift once
for (uint64_t i = tabSize - 1; i > 0; i--)
{
tab[i] = tab[i - 1];
}
tab[0] = lastValue;
}
}
/**
* @brief swap The lowest value of a given tab
* with the last
*
* @param tab
* @param tabSize
*/
void swapLowestWithLast(uint64_t* tab, uint64_t tabSize)
{
swapIndex(tab, getTabSmallestValueIndex(tab, tabSize), tabSize - 1);
}
/**
* @brief Fill a tab with unique value randomly spaced
*
* @param tab
* @param tabSize
*/
void fillTabUniformRandom(uint64_t* tab, uint64_t tabSize)
{
// Fill tab with values from 0 to tabSize
// randomly swap 2 indexes n times
for (uint64_t i = 0; i < tabSize; i++)
{
tab[i] = i;
}
shuffleTab(tab, tabSize, 1000);
}
/**
* @brief Get smallest value's index of an array
*
......@@ -52,7 +177,7 @@ void fillTabRandom(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max)
* @param tabSize
* @return uint64_t
*/
uint64_t getTabSmallestValue(uint64_t *tab, uint64_t tabSize)
uint64_t getTabSmallestValueIndex(uint64_t *tab, uint64_t tabSize)
{
uint64_t value = tab[0];
uint64_t index = 0;
......@@ -75,7 +200,7 @@ uint64_t getTabSmallestValue(uint64_t *tab, uint64_t tabSize)
* @param tabSize
* @return uint64_t
*/
uint64_t getTabBiggestValue(uint64_t *tab, uint64_t tabSize)
uint64_t getTabBiggestValueIndex(uint64_t *tab, uint64_t tabSize)
{
uint64_t value = 0;
uint64_t index = 0;
......@@ -92,7 +217,7 @@ uint64_t getTabBiggestValue(uint64_t *tab, uint64_t tabSize)
}
/**
* @brief Sort an array Low to High Selection
* @brief Sort an array Low to High
*
* @param tab
* @param tabSize
......@@ -105,12 +230,127 @@ void sortTabLTH(uint64_t *tab, uint64_t tabSize)
for (uint64_t i = 0; i < tabSize; i++)
{
tmp_v = tab[i];
min_i = getTabSmallestValue(tab+i, tabSize-i) ;
min_i = getTabSmallestValueIndex(tab+i, tabSize-i) ;
tab[i] = tab[min_i + i];
tab[min_i + i] = tmp_v;
}
}
/**
* @brief Sort Low to High with insertion algorithm
* ! source : https://en.wikipedia.org/wiki/Insertion_sort#Algorithm
* @param tab
* @param tabSize
*/
void sortInsertionLTH(uint64_t *tab, uint64_t tabSize)
{
uint64_t i = 1, j;
while(i < tabSize)
{
j = i;
// Shift the value while its not at the right place
while( (j > 0) && ( tab[j - 1] > tab[j] ) )
{
swapIndex(tab, j, j - 1);
j --;
}
i++;
}
}
/**
* @brief Sort High to low with insertion algorithm
*
* @param tab
* @param tabSize
*/
void sortInsertionHTL(uint64_t *tab, uint64_t tabSize)
{
uint64_t j;
for (uint64_t i = 1; i < tabSize; i++)
{
j = i;
// Shift the value while its not at the right place
// Or its the last index
while( (j > 0) && ( tab[j - 1] < tab[j] ) )
{
swapIndex(tab, j, j - 1);
j--;
}
}
}
/**
* @brief Get the Number Of Smaller Elements from an value
*
* @param tab
* @param tabSize
* @param element
*/
uint64_t getNumberOfSmallerElements(uint64_t* tab, uint64_t tabSize, uint64_t valElement)
{
uint64_t res = 0;
for (uint64_t i = 0; i < tabSize; i++)
{
if(tab[i] < valElement)
{
res++;
}
}
return res;
}
/**
* @brief Sum the values element by element of 2 tabs
* returns a pointer to a new tab
* @param tab1
* @param tab2
* @param tabSize
* @return uint64_t*
*/
uint64_t* sumTabs(uint64_t* tab1, uint64_t* tab2, uint64_t tabSize)
{
uint64_t* tabBuff = createTab(tabSize);
for (uint64_t i = 0; i < tabSize; i++)
{
tabBuff[i] = tab1[i] + tab2[i];
}
return tabBuff;
}
uint64_t* mulTabs(uint64_t* tab1, uint64_t multiplier, uint64_t tabSize)
{
uint64_t* tabBuff = createTab(tabSize);
for (uint64_t i = 0; i < tabSize; i++)
{
tabBuff[i] = tab1[i] * multiplier;
}
return tabBuff;
}
double* convertToDouble(uint64_t* tab, uint64_t tabSize)
{
// CREATE DOUBLE TAB
double* tabBuff = malloc(tabSize * sizeof(double));
if(tabBuff == NULL)
{
printf("MEMORY ALLOCATION FAILED\n");
exit(0);
}
for (uint64_t i = 0; i < tabSize; i++)
{
tabBuff[i] = (double)tab[i];
}
return tabBuff;
}
/**
* @brief Returns index of specified element in an array
*
......@@ -205,7 +445,7 @@ void countOccurence(uint64_t *tab, uint64_t tabSize, uint64_t *occurences, uint6
*/
void showVerticalHistoOfTab(uint64_t *occurences, uint64_t tabSize)
{
uint64_t topValue = occurences[getTabBiggestValue(occurences, tabSize)];
uint64_t topValue = occurences[getTabBiggestValueIndex(occurences, tabSize)];
// PRINT ACTUAL BARS
for(uint64_t i = 0; i < tabSize; i++)
{
......@@ -240,7 +480,7 @@ void showVerticalHistoOfTab(uint64_t *occurences, uint64_t tabSize)
*/
void showHorizonalHistoOfTab(uint64_t *occurences, uint64_t tabSize)
{
uint64_t topValue = occurences[getTabBiggestValue(occurences, tabSize)];
uint64_t topValue = occurences[getTabBiggestValueIndex(occurences, tabSize)];
// PRINT ACTUAL BARS
for(uint64_t i = topValue; i > 0; i--) // From topValue to 1
......@@ -284,26 +524,3 @@ void fillTabWithZeroes(uint64_t *tab, uint64_t tabSize)
tab[i] = 0;
}
}
/**
* @brief Get the pos where to place the element
*
* @param tab
* @param tabSize
* @return uint64_t
*/
uint64_t position(uint64_t *tab, uint64_t tabSize, uint64_t val)
{
uint64_t currentPos = 0;
while( (currentPos < tabSize) && (val < tab[currentPos]))
{
currentPos++;
}
return c urrentPos;
}
void shift(uint64_t *tab, uint64_t tabSize, uint64_t pos)
{
}
......@@ -13,61 +13,25 @@
#include <stdint.h>
#include "../include/jtableaux.h"
#include "../include/sort.h"
int main()
{
uint64_t sizeOfTab;
uint64_t frac_sizeOfTab;
srand(time(NULL)); // Create seed
printf("--------------------------------------------------\n");
printf(" _ _____ _ ___ _ ___ _ _ ___ __\n");
printf(" _ | |_ _/_\\ | _ ) | | __| /_\\| | | \\ \\/ /\n");
printf("| || | | |/ _ \\| _ \\ |__| _| / _ \\ |_| |> <\n");
printf(" \\__/ |_/_/ \\_\\___/____|___/_/ \\_\\___//_/\\_\\\n\n");
printf("Jonas Stirnemann\n");
printf("--------------------------------------------------\n");
// USER INPUT
printf("SIZE OF TAB : ");
scanf(" %ld", &sizeOfTab);
frac_sizeOfTab = sizeOfTab / 10 - 1;
// FIND ELEMENT
uint64_t element;
printf("ELEMENT : ");
scanf(" %ld", &element);
// RANDOM TAB
// uint64_t* tab = createTab(sizeOfTab);
uint64_t tab[sizeOfTab];
printf("\nFILLING TAB WITH RANDOM VALUES ... \n");
fillTabRandom(tab, sizeOfTab, 0, frac_sizeOfTab);
// SORT
printf("SORTING TAB ... \n\n");
sortTabLTH(tab, sizeOfTab);
printf("INDEX OF CHOSEN ELEMENT : %ld\n", getIndexOfTabElement(tab, sizeOfTab, element));
// GET MEAN
printf("MOYENNE : %.2lf\n", getMeanOfTab(tab, sizeOfTab));
// GET VARIANCE
printf("VARIANCE : %.2lf\n", getVarianceOfTab(tab, sizeOfTab, getMeanOfTab(tab, sizeOfTab)));
// GET MEDIAN
printf("MEDIAN : %ld\n\n", getMedianOfTab(tab, sizeOfTab));
// SHOW HISTOS
uint64_t occurences[frac_sizeOfTab];
countOccurence(tab, sizeOfTab, occurences, frac_sizeOfTab);
fillTabRandomValues(tab, sizeOfTab, 0, sizeOfTab);
printf("HISTO VERTICAL\n");
showVerticalHistoOfTab(occurences, frac_sizeOfTab);
printf("\n\n");
printf("HISTO VERTICAL\n");
showHorizonalHistoOfTab(occurences, frac_sizeOfTab);
showTab(tab, sizeOfTab, "UNSORTED TAB");
bubbleSort(tab, sizeOfTab);
showTab(tab, sizeOfTab, "SORTED TAB");
destroyTab(tab);
return 0;
}
/*
* CODE SORT
* Author : Jonas S.
* Date : 10/11/2021
! DESCRIPTION
*/
#include "../include/sort.h"
#include "../include/jtableaux.h" // swap index
// -------------------- UTILS --------------------
/**
* @brief Swap 2 values
*
* @param a
* @param b
*/
void swap(uint64_t* a, uint64_t* b)
{
uint64_t tmp = *a;
*a = *b;
*b = tmp;
}
//-------------------- INSERTION SORT --------------------
// N**2
// /**
// * @brief Sort Low to High with insertion algorithm
// * ! source : https://en.wikipedia.org/wiki/Insertion_sort#Algorithm
// * @param tab
// * @param tabSize
// */
// void sortInsertionLTH(uint64_t *tab, uint64_t tabSize)
// {
// uint64_t i = 1, j;
// while(i < tabSize)
// {
// j = i;
// // Shift the value while its not at the right place
// while( (j > 0) && ( tab[j - 1] > tab[j] ) )
// {
// swapIndex(tab, j, j - 1);
// j --;
// }
// i++;
// }
// }
// /**
// * @brief Sort High to low with insertion algorithm
// *
// * @param tab
// * @param tabSize
// */
// void sortInsertionHTL(uint64_t *tab, uint64_t tabSize)
// {
// uint64_t j;
// for (uint64_t i = 1; i < tabSize; i++)
// {
// j = i;
// // Shift the value while its not at the right place
// // Or its the last index
// while( (j > 0) && ( tab[j - 1] < tab[j] ) )
// {
// swapIndex(tab, j, j - 1);
// j--;
// }
// }
// }
//-------------------- QUICKSORT --------------------
/**
* @brief
*
* @param tab
* @param tabSize
* @param first First index of (sub)array
* @param last Last index of (sub)array
* @return uint64_t
*/
uint64_t partition(uint64_t* tab, uint64_t tabSize, uint64_t first, uint64_t last)
{
uint64_t pivot = tab[last];
uint64_t i = first - 1, j = last;
do
{
do
{
i++;
} while (tab[i] < pivot && i < j) ;
do
{
j--;
} while (tab[j] > pivot && j > i);
} while (j > i);
swap(&tab[i], &tab[j]);
return i;
}
void quicksort(uint64_t* tab, uint64_t tabSize, uint64_t first, uint64_t last)
{
if(first < last)
{
uint64_t midpoint = partition(tab, tabSize, first, last);
if(first < midpoint - 1)
{
quicksort(tab, tabSize, first, midpoint -1);
}
if(midpoint + 1 < last)
{
quicksort(tab, tabSize, midpoint - 1, last);
}
}
}
void bubbleSort(uint64_t* tab, uint64_t tabSize)
{
bool sorted = 0;
for (uint64_t i = tabSize - 1; i > 1; i--)
{
sorted = true;
for (uint64_t j = 0;j < i - 1; j++)
{
if(tab[j] > tab[j + 1])
{
swap(&tab[i], &tab[i + 1]);
sorted = false;
}
}
if(sorted)
{
return;
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment