int isStackSortable(int * array, int len) { if(len < 3) { return TRUE ; } int max_index = max_value(array, len) ; int left_max ; int right_min ; if(max_index == 0) { left_max = 0 ; right_min = array[min_value(array+1,len-1) + 1] ; if(left_max > right_min) { return FALSE ; } return isStackSortable(array+1, len-1) ; } if(max_index == (len-1) ) { left_max = array[max_value(array,len-1)]; right_min = array[len-1] ; if(left_max > right_min) { return FALSE ; } return isStackSortable(array, len-1) ; } left_max = array[max_value(array,max_index)] ; right_min = array[min_value(array+max_index+1,(len -max_index - 1) )+ max_index+1] ; if(left_max > right_min) { return FALSE ; } int left = isStackSortable(array,max_index) ; int right = isStackSortable(array+max_index+1,(len-max_index-1)) ; if(left == TRUE && right == TRUE) { return TRUE ; } else { return FALSE ; } }
void permuteHelper ( int * array , int len , int ind ) { // TreeNode * root = NULL ; int check ; if ( ind == len ) { check = isStackSortable( array, len) ; if(check == 1) { TreeNode * root = Tree_build(array, len) ; Tree_printShape(root) ; // printPermutation ( array , len ); Tree_destroy(root) ; } } int pos ; for ( pos = ind ; pos < len ; pos ++) { swap (& array [ pos ] , & array [ ind ]); permuteHelper ( array , len , ind + 1); swap (& array [ pos ] , & array [ ind ]); } }
void permutehelper(int * array, int index, int length) { if(index == length) { int sortable = isStackSortable(array, length); if(sortable == TRUE) { TreeNode * root = Tree_build(array, length); Tree_printShape(root); Tree_destroy(root); return; } else {return;} } int pos; for(pos = index; pos < length; pos++) { swap(&array[pos], &array[index]); permutehelper(array, index + 1, length); swap(&array[pos], &array[index]); } }
int main(int argc, char * * argv) { if(argc != 3) { printUsage(argv[0]); if(argc == 2 && strcmp("--help", argv[1]) == 0) return EXIT_SUCCESS; return EXIT_FAILURE; } int ret = EXIT_SUCCESS; // until otherwise noted const char * cmd = argv[1]; int * array = NULL; int len = 0; int i; // Read array if integers (if applicable) if(strcmp(cmd, "sort") == 0 || strcmp(cmd, "sortable") == 0) { const char * arrstr = argv[2]; if(!checkArrayStr(arrstr)) { fprintf(stderr, "Invalid array, aborting\n"); return EXIT_FAILURE; } len = strlen(arrstr); array = malloc(sizeof(int) * len); for(i = 0; i < len; ++i) array[i] = arrstr[i] - '0'; } if(strcmp(cmd, "sort") == 0) { // -- Sort command stackSort(array, len); for(i = 0; i < len; ++i) printf("%d", array[i]); printf("\n"); } else if(strcmp(cmd, "sortable") == 0) { // -- Sortable command if(isStackSortable(array, len)) printf("Y\n"); else printf("N\n"); } else if(strcmp(cmd, "shapes") == 0) { // -- Shapes command len = strtol(argv[2], NULL, 10); if(len < 1 || len > 9) { fprintf(stderr, "Invalid number of shapes... aborting\n"); ret = EXIT_FAILURE; } else { genShapes(len); } } else { // -- An error fprintf(stderr, "Invalid command: '%s', aborting\n", cmd); } free(array); return ret; }