Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions c++/insertion-sort.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <algorithm>

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<int> 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<int>::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;
}
85 changes: 85 additions & 0 deletions c/bubble-sort.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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<N;i++){
vector[i] = rand() % MAX_VALUE;
printf(" %d ", vector[i]);
}
printf("] \n Sorting... \n");

//bubble sort
j = 0;
isSorted = 0;
while(!isSorted){
isSorted = 1;
for(i = 0; i < N-1; i++){
j = i + 1;
if(vector[i] > vector[j]){
temp = vector[j];
vector[j] = vector[i];
vector[i] = temp;
isSorted = 0;
}
}
}

//print it sorted
printf("[");
for(i=0;i<N;i++){
printf(" %d ", vector[i]);
}
printf("] \n");

//free memory
free(vector);
}

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]);
printf("Vector size: %d \n",*N);
return 0;
}
return -1;
}
85 changes: 85 additions & 0 deletions c/comparison-sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
comparison-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 comparison-sort.c -o exec

To run: ./exec vectorSize MAX_VALUE
Example: ./comparison 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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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<N;i++){
vector[i] = rand() % MAX_VALUE;
printf(" %d ", vector[i]);
}
printf("] \n Sorting... \n");

//comparison sort
j = 0;
for(i = 0; i < N-1; i++){
smaller = i;
for(j = i+1; j < N; j++){
if(vector[smaller] > 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<N;i++){
printf(" %d ", vector[i]);
}
printf("] \n");

//free memory
free(vector);
}

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]);
printf("Vector size: %d \n",*N);
return 0;
}
return -1;
}
78 changes: 78 additions & 0 deletions c/insertion-sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
insertion-sort.c

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:

gcc insertion-sort.c -o exec

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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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<N;i++){
vector[i] = rand() % MAX_VALUE;
printf(" %d ", vector[i]);
}
printf("]\n Sorting... \n");

//insertion sort
j = 0;
for(i = 0; i < N-1; i++){
j = i + 1;
while(j > 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<N;i++){ printf(" %d ", vector[i]); }
printf("] \n");

//free memory
free(vector);
}

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]);
printf("Vector size: %d \n",*N);
return 0;
}
return -1;
}