diff --git a/c++/insertion-sort.cpp b/c++/insertion-sort.cpp new file mode 100644 index 0000000..279b2da --- /dev/null +++ b/c++/insertion-sort.cpp @@ -0,0 +1,76 @@ +/* + insertion-sort.cpp + + Created by Iran Neto on 20/10/18. + + This code implements the insertion sort algorithm with positive numbers. + It works by taking one element, analyzing the vector to define where insert it. + To compile you must use g++ or gcc and also set a flag to c++11: + + g++ insertion-sort.c -o exec -std=c++11 + + To run: ./exec vectorSize MAX_VALUE + Example: ./insertion 10 100 + + Extra: Use time command from Unix Operating Systems for efficiency measurements + + Best case: Vector already sorted - O(n) + Worst case: Vector is reversed order = O(n²) +*/ + +#include +#include +#include +#include +#include + +int MAX_VALUE; //needs to be global + +int getParameters(int* N, int* MAX_VALUE, int argc, char* argv[]){ + //get parameters + if(argc == 3){ + *N = atoi(argv[1]); + *MAX_VALUE = atoi(argv[2]); + std::cout << "Vector size: "<< *N << std::endl; + return 0; + } + return -1; +} + +int randIt(){ return (std::rand()%MAX_VALUE); } + +int main(int argc, char* argv[]){ + int N, res; + srand(time(NULL)); + + res = getParameters(&N,&MAX_VALUE,argc,argv); + if(res != 0){ + std::cout << "Specify size and max_value... " << std::endl; + std::cout << " ./exec vectorSize MAX_VALUE" << std::endl; + exit(1); + } + + std::vector vec(N); + std::generate(vec.begin(), vec.end(), randIt); + + std::cout << "["; + for(auto it: vec){ std::cout << " " << it << " ";} + std::cout << "]" << std::endl << " Sorting... " << std::endl; + + /* vector.end() == V[N] - vector last element is in vector.end()-1 */ + //insertion-sort + std::vector::iterator it, it2, temp; + for(it = vec.begin(); it != vec.end()-1; ++it){ + it2 = it + 1; + std::cout << &it << "- it: " << *it << " " << &it2 << " - it2: " << *it2 << " - it2-1: " << *(it2-1) << std::endl; + while(it2 != vec.begin() && *it2 < *(it2 - 1)){ + std::swap(*(it2-1),*temp); + --it2; + } + } + + //print it sorted + std::cout << "["; + for(auto it: vec){ std::cout << " " << it << " ";} + std::cout << "]" << std::endl; +} \ No newline at end of file diff --git a/c/bubble-sort.c b/c/bubble-sort.c new file mode 100644 index 0000000..62950ef --- /dev/null +++ b/c/bubble-sort.c @@ -0,0 +1,85 @@ +/* + bubble-sort.c + + Created by Iran Neto on 20/10/18. + + This code implements the insertion sort algorithm with positive numbers. + It works by taking pairs of elements, analyzing them and swap them if they're unordered. + To compile you must use g++ or gcc: + + gcc bubble-sort.c -o exec + + To run: ./exec vectorSize MAX_VALUE + Example: ./bubble 10 100 + + Extra: Use time command from Unix Operating Systems for efficiency measurements + + Best case: Vector already sorted - O(n) + Worst case: Vector is reversed order = O(n²) +*/ + +#include +#include +#include + +int getParameters(int* N, int* MAX_VALUE, int argc, char* argv[]); + +int main(int argc, char *argv[]){ + int* vector; + int temp, i, j, N, MAX_VALUE, isSorted, res; + + srand(time(NULL)); + + res = getParameters(&N,&MAX_VALUE,argc,argv); + if(res != 0){ + printf("Specify size and max_value... \n ./exec vectorSize MAX_VALUE"); + exit(1); + } + + vector = (int*) malloc(N*sizeof(int)); + + //fill vector and print it + printf("["); + for(i=0;i vector[j]){ + temp = vector[j]; + vector[j] = vector[i]; + vector[i] = temp; + isSorted = 0; + } + } + } + + //print it sorted + printf("["); + for(i=0;i +#include +#include + +int getParameters(int* N, int* MAX_VALUE, int argc, char* argv[]); + +int main(int argc, char *argv[]){ + int* vector; + int temp, i, j, N, MAX_VALUE, smaller, res; + + srand(time(NULL)); + + res = getParameters(&N,&MAX_VALUE,argc,argv); + if(res != 0){ + printf("Specify size and max_value... \n ./exec vectorSize MAX_VALUE"); + exit(1); + } + + vector = (int*) malloc(N*sizeof(int)); + + //fill vector and print it + printf("["); + for(i=0;i vector[j]){ + smaller = j; + } + } + if(vector[smaller] != vector[i]){ + temp = vector[i]; + vector[i] = vector[smaller]; + vector[smaller] = temp; + } + } + + //print it sorted + printf("["); + for(i=0;i +#include +#include + +int getParameters(int *N, int *MAX_VALUE, int argc, char* argv[]); + +int main(int argc, char *argv[]){ + int* vector; + int temp, i, j, N, MAX_VALUE, res; + + srand(time(NULL)); + + res = getParameters(&N,&MAX_VALUE,argc,argv); + if(res != 0){ + printf("Specify size and max_value... \n ./exec vectorSize MAX_VALUE"); + exit(1); + } + + vector = (int*) malloc(N*sizeof(int)); + + printf("["); + for(i=0;i 0 && vector[j] < vector[j-1]){ + temp = vector[j]; + vector[j] = vector[j-1]; + vector[j-1] = temp; + j--; + } + } + + //print it sorted + printf("["); + for(i=0;i