Skip to content

Instantly share code, notes, and snippets.

@YounesCheikh
Last active December 23, 2015 16:49
Show Gist options
  • Save YounesCheikh/6664955 to your computer and use it in GitHub Desktop.
Save YounesCheikh/6664955 to your computer and use it in GitHub Desktop.
C'est à toi de finir :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define MAX 300
int t[MAX];
int n;
// Exo 1
int Fib(int n ) {
int retVal = 0;
if ( n >= 0 ) {
if ( n == 0 || n == 1 ) retVal = n;
if ( n != retVal ) {
retVal = Fib( n -2 ) + Fib(n-1);
}
}
return retVal;
}
// Fill table with random integers
void fillTable() {
int i = 0 ;
for(i = 0; i<n; i++) {
t[i] = rand()%1000+1;
}
}
bool isBlue(int i) {
return t[i] % 2 == 0;
}
bool isWhite( int i ) {
return t[i] %2 != 0 && t[i]%3 == 0;
}
bool isRed( int i ) {
return !isBlue(i) && !isWhite(i);
}
void permute(int i , int j) {
int tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
void sortMyTable() {
int i, j ;
int lastUsedIndex = 0;
// Sort blue
// Looking for the index of the first occurance non-blue
for( i = 0 ; i< n ; i++) {
if( isBlue(i) ) {
for( j = 0; j<i ; j++) {
if (!isBlue(j)) {
permute(i,j);
lastUsedIndex = j;
break;
}
}
}
}
// Sort red
// Looking for the index of the first occurance non-red
for( i = n-1 ; i> lastUsedIndex ; i--) {
if( isRed(i)) {
for( j = n-1; j>lastUsedIndex ; j--) {
if (isWhite(j)) {
permute(i,j);
break;
}
}
}
}
// The white range is now sorted :)
}
int main(void) {
//srand(time(NULL));
srand( (unsigned int)(time(NULL)));
n = 10;
fillTable();
int i = 0 ;
for(i = 0 ; i< n ; i++)
printf("%d ", t[i]);
printf("\n");
sortMyTable();
for(i = 0 ; i< n ; i++)
printf("%d ", t[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment