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

quick buble stack ok

parent 29030b17
Branches
No related tags found
No related merge requests found
cc=gcc
LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm
buble.x: buble_sort.o main.o
$(cc) -o $@ $^
buble_sort.o: buble_sort.c sort.h
$(cc) -c $^
quick.x: quick_sort.o main.o
$(cc) -o $@ $^
quick_sort.o: quick_sort.c sort.h
$(cc) -c $^
stack.x: stack_sort.o main.o
$(cc) -o $@ $^
stack_sort.o: stack_sort.c sort.h
$(cc) -c $^
main.o: main.c
$(cc) -c $<
clean:
rm -f *.o *.x
#include "sort.h"
#include <stdio.h>
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");
return false;
}
}
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++) {
tab[i] = min+rand()%(max-min);
}
}
bool isGreater(int a, int b) { return a > b; }
bool isSmaller(int a, int b) { return a < b; }
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void print_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) {
// copy
for (int i = 0; i < nitems; i++) {
sorted[i] = orig[i];
}
for (int i = nitems; i > 1; i--) {
for (int i = 0; i < nitems - 1; i++) {
if (comp(sorted[i], sorted[i + 1])) {
swap(&sorted[i], &sorted[i + 1]);
}
}
}
}
\ No newline at end of file
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
int main(){
int size=1000;
int orig[size];
int sorted[size];
random_tab(size, orig, 1, 500);
sort(sorted, orig, size, isSmaller);
isSorted(size, sorted, isSmaller);
print_array(orig,size);
print_array(sorted,size);
return EXIT_SUCCESS;
}
#include "sort.h"
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");
return false;
}
}
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++) {
tab[i] = min+rand()%(max-min);
}
}
bool isGreater(int a, int b) { return a > b; }
bool isSmaller(int a, int b) { return a < b; }
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void print_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Partition du tableau <array> autour d'une valeur pivot:
int partition(int size, int array[size], int first, int last,bool (*comp)(int, int)) {
int pivot = array[last];
int i = first;
int j = last-1;
while (true) {
while (comp(array[i],pivot)){
i++;
} //array[i]<pivot
while (!comp(array[j],pivot)){
j--;
} //array[j]>pivot
if (i >= j) {
break;
}
// échanger cases <i> et <j> du tableau <array>
swap(&array[i], &array[j]);
}
// échanger cases <i> et <last> du tableau <array>
swap(&array[i], &array[last]);
return i;
}
// Tri rapide récursif
void quicksort(int size, int array[size], int first, int last,bool (*comp)(int, int)) {
if (first < last) {
int midpoint = partition(size, array, first, last,comp);
if (first < midpoint - 1) {
quicksort(size, array, first, midpoint - 1,comp);
}
if (midpoint + 1 < last) {
quicksort(size, array, midpoint + 1, last,comp);
}
}
}
void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) {
// copy
for (int i = 0; i < nitems; i++) {
sorted[i] = orig[i];
}
quicksort(nitems, sorted, 0, nitems-1,comp);
}
#ifndef _SORT_H_
#define _SORT_H_
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
bool isSorted(int size, int tab[size], bool (*comp)(int, int));
void random_tab(int size,int tab[size],int min,int max);
bool isGreater(int a, int b);
bool isSmaller(int a, int b);
void swap(int *a, int *b);
void print_array(int *arr, int size);
void sort(int *sorted, const int *const orig, int nitems,bool (*comp)(int, int));
#endif
\ No newline at end of file
#include "sort.h"
#include <stdlib.h>
typedef struct _stack {
int *data;
int top;
int capacity;
} stack;
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");
return false;
}
}
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++) {
tab[i] = min + rand() % (max - min);
}
}
bool isStackEmpty(stack stack) { return stack.top == -1; }
bool isStackFull(stack stack) { return stack.capacity - 1 == stack.top; }
int stack_top(stack stack) { return stack.data[stack.top]; }
int stack_count(stack stack) { return stack.top + 1; }
void stack_init(stack *stack, int max) {
stack->data = malloc(sizeof(int) * max);
stack->top = -1;
stack->capacity = max;
}
void stack_destroy(stack *stack) {
free(stack->data);
stack->top = -1;
stack->capacity = -1;
}
void stack_add(stack *stack, int item) {
if (!isStackFull(*stack)) {
stack->top++;
stack->data[stack->top] = item;
}
}
int stack_remove(stack *stack) {
int tmp;
tmp = stack->data[stack->top];
stack->top--;
return tmp;
}
bool isGreater(int a, int b) { return a > b; }
bool isSmaller(int a, int b) { return a < b; }
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void print_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void transfer(stack *a, stack *b) { stack_add(b, stack_remove(a)); }
void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) {
stack g, d;
stack_init(&g, nitems);
stack_init(&d, nitems);
stack_add(&g, orig[0]);
for (int i = 1; i < nitems; i++) {
// transfère les éléments de <pile_g> à <pile_d> // tant que sommet(pile_g)
// < tab[i]
while (comp(stack_top(g), orig[i]) && !isStackEmpty(g)) { //stack_top(g) < orig[i]
transfer(&g, &d);
}
while (!comp(stack_top(d) , orig[i]) && !isStackEmpty(d)) { //stack_top(d) >= orig[i]
transfer(&d, &g);
}
// empile l'élément dans <pile_g>
stack_add(&g, orig[i]);
}
while (!isStackEmpty(d)) {
transfer(&d, &g);
}
for (int i = 0; i < nitems; i++) {
sorted[i] = stack_remove(&g);
}
stack_destroy(&d);
stack_destroy(&g);
}
\ 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