void bucket_sort(int *nums, int len) { int i, index, max; node_t *buckets[len], *head; for (i = 0; i < len; ++i) { buckets[i] = NULL; } // Put into bucket. max = max_num(nums, len) + 1; for (i = 0; i < len; ++i) { index = nums[i] * len / max; head = buckets[index]; buckets[index] = list_insert(head, nums[i]); } // Back to array. index = 0; head = buckets[index]; for (i = 0; i < len && index < len - 1; ++i) { while (index < len - 1 && NULL == head) { head = buckets[++index]; } nums[i] = head->value; head = head->next; } }
void counting_sort(int *nums, int len) { int i, tmp, max = max_num(nums, len); int *cnums = (int *)malloc(max * sizeof(int)); int *bp = nums; for(i=0; i < max; i++){ cnums[i] = 0; } while((len--) > 0){ cnums[*bp - 1] += 1; bp++; } print(cnums, max); bp = nums; for(i=0; i < max; i++){ tmp = cnums[i]; while((tmp--) > 0){ *bp++ = i + 1; } } free(cnums); cnums = NULL; }
int main(){ int n1=25; int n2=99; int max = max_num(n1, n2); printf("The Max Value Is %d\n", max); printf("The Min Value Is %d\n", min_num(n1,n2)); return 0; }
void BankRBTree::runRecommend(string id, string oid, int len, RecommendId& rid, int degree_c, int degree_a) { if (degree_c == 0 && degree_a == 0) { string now_id = id; DataNode la = DataNode(&now_id, NULL); DataNode* res = (DataNode*)rb_find(rb_tree, &la); if (res == NULL) { // it means that the id isn't exist in the bank rid.push_back(id); return; } } string tmpid; int delta = abs_num((int)oid.length() - (int)id.length()); if (degree_a > delta) { if (id.length() >= oid.length() && id.length() < MAX_STRING_SIZE) { for (char c = '0'; ; c = next_char(c)) { runRecommend(id + c, oid, id.length() + 1, rid, degree_c, degree_a - delta - 1); if (c == 'z') break; } } if (id.length() <= oid.length() && id.length() > len && id.length() > 1) { tmpid = id; tmpid.pop_back(); runRecommend(tmpid, oid, len, rid, degree_c, degree_a - delta - 1); } } if (degree_c > 0) { for (int i = max_num(min_num(id.length(), oid.length()) - degree_c, len); i < min_num(oid.length(), id.length()); i++) { if (degree_c >= min_num(oid.length(), id.length()) - i) { for (char c = '0'; c <= 'z'; c = next_char(c)) { if (c != oid[i]) { tmpid = id; tmpid[i] = c; runRecommend(tmpid, oid, i + 1, rid, degree_c - (min_num(oid.length(), id.length()) - i), degree_a); } if (c == 'z') break; } } } } }
void BankUMap::runRecommend(string id, string oid, int len, RecommendId& rid, int degree_c, int degree_a) { if (degree_c == 0 && degree_a == 0) { UMap::iterator it = umap.find(id); if (it == umap.end()) { // it means that the id isn't exist in the bank rid.push_back(id); return; } } string tmpid; int delta = abs_num((int)oid.length() - (int)id.length()); if (degree_a > delta) { if (id.length() >= oid.length() && id.length() < MAX_STRING_SIZE) { for (char c = '0'; ; c = next_char(c)) { runRecommend(id + c, oid, id.length() + 1, rid, degree_c, degree_a - delta - 1); if (c == 'z') break; } } if (id.length() <= oid.length() && id.length() > len && id.length() > 1) { tmpid = id; tmpid.pop_back(); runRecommend(tmpid, oid, len, rid, degree_c, degree_a - delta - 1); } } if (degree_c > 0) { for (int i = max_num(min_num(id.length(), oid.length()) - degree_c, len); i < min_num(oid.length(), id.length()); i++) { if (degree_c >= min_num(oid.length(), id.length()) - i) { for (char c = '0'; c <= 'z'; c = next_char(c)) { if (c != oid[i]) { tmpid = id; tmpid[i] = c; runRecommend(tmpid, oid, i + 1, rid, degree_c - (min_num(oid.length(), id.length()) - i), degree_a); } if (c == 'z') break; } } } } }
int main( int argc, char *argv[] ) { bool print_usage_and_exit = false; bool generate = false; long number_count = 100; bool has_number_count = false; int number_min = 1; bool has_number_min = false; int number_max = 255; bool has_number_max = false; int seed = 0; bool has_seed = false; const char* ifile_name = NULL; const char* ofile_name = NULL; const char* cfile_name = NULL; ///overallocate the numbers a bit, just in case int* number_array = malloc(sizeof(int)*argc); size_t number_array_size = 0; if (!number_array) { printf("error allocating, somethings wrong\n"); exit(-1); } for (int i = 1; i < argc; ++i) { const char* argi = argv[i]; //if (atoi(argi) != 0 || arg_is_zero(argi)) if (atoi(argi) != 0 || arg_is_zero(argi)) { ///this was a number input number_array[number_array_size] = atoi(argi); number_array_size++; continue; } char dash = argi[0]; if (strlen(argi) != 2 || dash != '-') { print_usage(); return -1; } char flag = argi[1]; if (flag == 'u') { if (print_usage_and_exit) { ///Example: ./Lab1 -u -u print_usage(); return -1; } print_usage_and_exit = true; continue; } if (flag == 'g') { if (generate || number_array_size > 0) { ///Example: ./Lab1 -g -g ///Example: ./Lab1 -g 5 5 print_usage(); return -1; } generate = true; continue; } if (!(i + 1 < argc)) { ///Example: ./Lab1 -m print_usage(); return -1; } if (flag == 'n') { if (has_number_count) { ///Example: ./Lab1 -n <number> -n <number> print_usage(); return -1; //continue; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_count = true; number_count = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_count == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -n NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'm') { if (has_number_min) { ///Example: ./Lab1 -m <min> -m <min> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_min = true; number_min = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_min == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -m NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'M') { if (has_number_max) { ///Example: ./Lab1 -M <max> -M <max> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_number_max = true; number_max = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (number_max == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -M NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 's') { if (has_seed) { ///Example: ./Lab1 -s <seed> -s <seed> print_usage(); return -1; } const char* next_arg_i = argv[i+1]; char* next_arg_i_end = NULL; has_seed = true; seed = strtol( /*first character in string*/next_arg_i , /*this will change the end pointer point to the last character strtol parses as a number*/ &next_arg_i_end , /*base*/10); if (seed == 0 && arg_is_not_zero(next_arg_i)) { ///Example: ./Lab1 -s NOTANUMBER print_usage(); return -1; } i++; continue; } if (flag == 'i') { if (ifile_name != NULL) { ///Example: ./Lab1 -i INPUTFILE -i INPUTFILE print_usage(); return -1; } ifile_name = argv[i+1]; i++; continue; } if (flag == 'o') { if (ofile_name != NULL) { ///Example: ./Lab1 -o OUTPUTFILE -o OUTPUTFILE print_usage(); return -1; } ofile_name = argv[i+1]; i++; continue; } if (flag == 'c') { if (cfile_name != NULL) { ///Example: ./Lab1 -c OUTPUTFILE -c OUTPUTFILE print_usage(); return -1; } cfile_name = argv[i+1]; i++; continue; } } if (print_usage_and_exit) { print_usage(); exit(0); } if (ifile_name) { free(number_array); number_array = get_input_from_file(ifile_name, &number_array_size); } FILE* ofile = NULL; if (ofile_name) { ofile = fopen(ofile_name, "w"); if (!ofile) { printf("Cannot open %s for writing\n", ofile_name); exit(-1); } } FILE* cfile = NULL; if (cfile_name) { cfile = fopen(cfile_name, "w"); if (!cfile) { printf("Cannot open %s for writing\n", cfile_name); exit(-1); } } if (!generate && number_array_size == 0) { fprintf(stdout, "Enter the number count\n"); fflush(stdout); free(number_array); int ret = fscanf(stdin, "%zi", &number_array_size); fgetc(stdin); if (ret == EOF) { fprintf(stdout, "Error reading from command line\n"); exit(-1); } number_array = (int*)malloc(sizeof(int)*number_array_size); for (int i = 0; i < number_array_size; ++i) { int value; int ret = fscanf(stdin, "%i", &value); if (ret == EOF) { fprintf(stdout, "Error reading from command line\n"); exit(-1); } fgetc(stdin); number_array[i] = value; } } sort_nums(number_array, number_array_size); if (generate) { if(has_number_count) { if(has_number_max && has_number_min && !(has_seed) && !(ofile)) //3 min_num_Max(number_count, number_min, number_max); if(has_number_max && has_number_min && has_seed && ofile) // 1 min_num_Max_seed(number_count, number_min, number_max, seed, ofile); if(has_number_max && has_number_min && has_seed && !(ofile)) // 2 //7 min_num_Max_seed_Nf(number_count, number_min, number_max, seed); else if(has_number_max && !(has_number_min) && !(has_seed) && !(ofile)) // 15 max_num(number_count,number_max); else if (has_number_min &&!(has_number_max) && !(has_seed) && !(ofile)) //4 min_num(number_count,number_min); else if(has_number_max && !(has_number_min) && (has_seed)&& !(ofile)) //12 max_num_seed(number_count,number_max,seed); else if (has_number_min &&!(has_number_max) && (has_seed) && !(ofile) ) //5 min_num_seed(number_count,number_min,seed); else if (has_number_min &&!(has_number_max) && !(has_seed) && (ofile) ) //6 min_num_of(number_count,number_min,ofile); else if (has_number_min && (has_number_max) && !(has_seed) && (ofile) ) //8 // I have problem here min_max_num_of(number_count,number_min,number_max,ofile); else if (!(has_number_min) && (has_number_max) && !(has_seed) && (ofile) ) //9 // I am heve problem max_num_of(number_count,number_max,ofile); else if (!(has_number_min) && !(has_number_max) && (has_seed) && (ofile) ) //10 // completed num_seed_of(number_count,seed,ofile); else if (!(has_number_min) && !(has_number_max) && !(has_seed) && (ofile) ) //11 // result in negative num_of(number_count,ofile); else if(has_seed && !(has_number_min) && !(has_number_max) && !(ofile)) //14 // good random_case_generator_seed(number_count,seed); else if (!(has_number_min) && (has_number_max) && (has_seed) && (ofile) ) //13 // good max_num_of_seed(number_count,number_max,seed,ofile); if(has_number_count && !(has_seed) && !(has_number_max) && !(has_number_min) && !(ofile)) //16 random_num_generator(number_count); if (has_number_count) { goto here; } } if(!(has_number_count)) { if(has_number_max && has_number_min && !(has_seed) && !(ofile)) //3 /////////////// Done min_num_Max_nd(number_min, number_max); if(has_number_max && has_number_min && has_seed && ofile) // 1 /////////////////Done min_num_Max_seed_nd( number_min, number_max, seed, ofile); //done if(has_number_max && has_number_min && has_seed && !(ofile)) // 2 //7 ////////////// Done min_num_Max_seed_Nf_nd( number_min, number_max, seed); else if(has_number_max && !(has_number_min) && !(has_seed) && !(ofile)) // 15 /////////// Done max_num_nd(number_max); else if (has_number_min &&!(has_number_max) && !(has_seed) && !(ofile)) //4 //////////// Done min_num_nd(number_min); else if(has_number_max && !(has_number_min) && (has_seed)&& !(ofile)) //12 /////////// Done max_num_seed_nd(number_max,seed); else if (has_number_min &&!(has_number_max) && (has_seed) && !(ofile) ) //5 /////////// Done min_num_seed_nd(number_min,seed); else if (has_number_min &&!(has_number_max) && !(has_seed) && (ofile) ) //6 //////////// Done min_num_of_nd(number_min,ofile); else if (has_number_min && (has_number_max) && !(has_seed) && (ofile) ) //8 //////////// Done min_max_num_of_nd(number_min,number_max,ofile); else if (!(has_number_min) && (has_number_max) && !(has_seed) && (ofile) ) //9 //////////// Done max_num_of_nd(number_max,ofile); else if (!(has_number_min) && !(has_number_max) && (has_seed) && (ofile) ) //10 //////////// Done num_seed_of_nd(seed,ofile); else if (!(has_number_min) && !(has_number_max) && !(has_seed) && (ofile) ) //11 //////////// Problem num_of_nd(ofile); else if(has_seed && !(has_number_min) && !(has_number_max) && !(ofile)) //14 // good //////// Done random_case_generator_seed_nd(seed); else if (!(has_number_min) && (has_number_max) && (has_seed) && (ofile) ) //13 // good /////// Done max_num_of_seed_nd(number_max,seed,ofile); if (!(has_number_count) && !(has_seed) && !(has_number_max) && !(has_number_min) && !(ofile)) //// Done { random_case_generator(); } } here: printf(""); } else { ///get input from user const char* userid = getenv("USER"); size_t useridlen = strlen(userid); Begin_clock(); for (int i = 0; i < useridlen; ++i) { char c = userid[i]; int freq = calculate_frequency(c, number_array, number_array_size); if (cfile) fprintf(cfile, "%c %d\t%d\n", c, (int)c, freq); if (!cfile) fprintf(stdout, "%c %d\t%d\n", c, (int)c, freq); } if (ofile) { for (int i = 0; i < number_array_size; ++i) { fprintf(ofile, "%d\n", number_array[i]); } fprintf(ofile, "\n"); } if (!ofile) { for (int i = 0; i < number_array_size; ++i) { printf("\n"); fprintf(stdout, "%d\t", number_array[i]); } printf("\n"); } Eng_clock(); Calculate_Time(); } return 0; }