Skip to content
Snippets Groups Projects
Commit e5191ecd authored by thib's avatar thib
Browse files

mesure time

parent 26d8033e
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,15 @@ stack.x: stack_sort.o main.o
stack_sort.o: stack_sort.c sort.h
$(cc) -c $^
# insert.x: insert_sort.o main.o
# $(cc) -o $@ $^
# insert_sort.o: insert_sort.c sort.h
# $(cc) -c $^
# select.x: select_sort.o main.o
# $(cc) -o $@ $^
# select_sort.o: select_sort.c sort.h
# $(cc) -c $^
main.o: main.c
$(cc) -c $<
......
......@@ -5,15 +5,14 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)){
for( int i = 1; i<size;i++){
if(comp(tab[i-1],tab[i])){
printf("not sorted\n");
//printf("not sorted\n");
return false;
}
}
printf("sorted !\n");
//printf("sorted !\n");
return true;
}
void random_tab(int size,int tab[size],int min,int max) {
assert(max > min);
for (int i=0;i<size;i++) {
......@@ -46,12 +45,13 @@ void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)
sorted[i] = orig[i];
}
for (int i = nitems; i > 1; i--) {
for (int i = 0; i < nitems - 1; i++) {
for (int j = nitems; j > 1; j--) {
for (int i = 0; i < j; i++) {
if (comp(sorted[i], sorted[i + 1])) {
swap(&sorted[i], &sorted[i + 1]);
}
}
}
}
\ No newline at end of file
#include "sort.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
#include <time.h>
int main(){
int main() {
srand(time(NULL));
int size = 1000;
int orig[size];
int sorted[size];
struct timespec start, finish;
clock_gettime(CLOCK_MONOTONIC, &start);
// code à mesurer
for (int i = 0; i < 1000; i++) {
random_tab(size, orig, 1, 500);
sort(sorted, orig, size, isSmaller);
sort(sorted, orig, size, isGreater);
// assert(isSorted(size, sorted, isSmaller));
// sort(sorted, orig, size, isSmaller);
// assert(isSorted(size, sorted, isGreater));
}
clock_gettime(CLOCK_MONOTONIC, &finish);
double seconds_elapsed = finish.tv_sec - start.tv_sec;
seconds_elapsed += (finish.tv_nsec - start.tv_nsec) / 1.0e9;
printf("time elapsed : %f sec\n", seconds_elapsed);
isSorted(size, sorted, isSmaller);
print_array(orig,size);
print_array(sorted,size);
// print_array(orig,size);
// print_array(sorted,size);
return EXIT_SUCCESS;
}
......@@ -4,11 +4,11 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)){
for( int i = 1; i<size;i++){
if(comp(tab[i-1],tab[i])){
printf("not sorted\n");
//printf("not sorted\n");
return false;
}
}
printf("sorted !\n");
//printf("sorted !\n");
return true;
}
......
......@@ -13,11 +13,11 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)) {
for (int i = 1; i < size; i++) {
if (comp(tab[i - 1], tab[i])) {
printf("not sorted\n");
// printf("not sorted\n");
return false;
}
}
printf("sorted !\n");
//printf("sorted !\n");
return true;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment