- Notifications
You must be signed in to change notification settings - Fork 270
Add Counting Sort, Maximo Recursivo, Min Max Iterativo [C++] #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits Select commit Hold shift + click to select a range
a5a669e add counting sort
Poskvansk b0ad83e Add Max Recursivo
Poskvansk 6ad8332 update readme
Poskvansk 3cc707a Delete a.exe
Poskvansk 2afb4e7 Update MaximoRecursivo.cpp
Poskvansk f93448c Merge branch 'main' of https://github.com/kelvins/Algoritmos-e-Estrut…
Poskvansk 126be68 Add busca sequencial C++
Poskvansk 466fd48 Update README
Poskvansk ffefb4b Comeco codigo fila encadeada
Poskvansk 9cc6d16 Update src/cpp/BuscaSequencial.cpp
Poskvansk e2178d5 Add Lista Encadeada C++
Poskvansk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| | ||
| using namespace std; | ||
| | ||
| int busca_sequencial(vector<int> &nums, int target) { | ||
| | ||
| for (size_t i = 0; i < nums.size(); i++) { | ||
| | ||
| if(nums[i] == target) | ||
| return i; | ||
| } | ||
| | ||
| return -1; | ||
| } | ||
| | ||
| int main() { | ||
| | ||
| vector<int> nums = {1, 2, 3, 4, 5, 27, -1, 12, 999}; | ||
| int target; | ||
| | ||
| cout << "Digite o numero que deseja busca no vetor: "; | ||
| cin >> target; | ||
| cout << "\n"; | ||
| | ||
| int pos = busca_sequencial(nums, target); | ||
| | ||
| if(pos > -1) { | ||
| cout << "Numero encontrado no vetor na posicao: " << pos << endl; | ||
| } | ||
| else { | ||
| cout << "Numero nao encontrado no vetor." << endl; | ||
| } | ||
| | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| | ||
| using namespace std; | ||
| | ||
| vector<int> counting_sort(vector<int>& nums) { | ||
| | ||
| | ||
| // nums = {5, 0, 1, 2, 3, 5, 3, 0, 9, 4} | ||
| // max = 9 | ||
| // vector nums contem elementos entre [0 .. max] | ||
| auto max = *max_element(nums.begin(), nums.end()); | ||
| | ||
| | ||
| // cria um vector com max + 1 posicoes para contar ocorrências de cada elemento | ||
| // 0 1 2 3 4 5 6 7 8 9 -> elementos possiveis de ocorrer no vector nums | ||
| //counting = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | ||
| vector<int> counting(max+1); | ||
| | ||
| | ||
| // conta ocorrencias de cada elemento | ||
| // nums = {5, 0, 1, 2, 3, 5, 3, 0, 9, 4} | ||
| // | ||
| // 0 1 2 3 4 5 6 7 8 9 | ||
| // counting = {2, 1, 1, 2, 1, 2, 0, 0, 0, 1} | ||
| // | ||
| for(auto& i : nums) { | ||
| counting[i]++; | ||
| } | ||
| | ||
| | ||
| // agora, counting = {2, 3, 4, 6, 7, 9, 9, 9, 9, 10} | ||
| // sera utilizado para determinar as posicoes dos elementos no vector ordenado | ||
| for (size_t i = 1; i < counting.size(); i++) { | ||
| | ||
| counting[i] += counting[i-1]; | ||
| } | ||
| | ||
| vector<int> sorted(nums.size()); | ||
| | ||
| /* | ||
| | ||
| O proximo loop coloca os numeros em suas devidas posicoes no vetor ordenado | ||
| i = itera pelos elementos de nums | ||
| counting[i] -1 é a posicao que o elemento i devee assumir no vetor ordenado | ||
| | ||
| Os 3 primeiros passos seriam: | ||
| | ||
| nums = {5, 0, 1, 2, 3, 5, 3, 0, 9, 4} | ||
| counting = {2, 3, 4, 6, 7, 9, 9, 9, 9, 10} | ||
| sorted = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | ||
| | ||
| passo 1: | ||
| i = 5 | ||
| counting[i] => counting[5] => 9 | ||
| sorted[9 - 1] => sorted[8] = i => sorted[8] = 5 | ||
| sorted = {0, 0, 0, 0, 0, 0, 0, 0, 5, 0} | ||
| | ||
| passo 2: | ||
| i = 0 | ||
| counting[i] => counting[0] => 2 | ||
| sorted[2 - 1] => sorted[1] = i => sorted[1] = 0 | ||
| sorted = {0, 0, 0, 0, 0, 0, 0, 0, 5, 0} | ||
| | ||
| passo 3: | ||
| i = 1 | ||
| counting[i] => counting[1] => 3 | ||
| sorted[3 - 1] => sorted[2] = i => sorted[2] = 1 | ||
| sorted = {0, 0, 1, 0, 0, 0, 0, 0, 5, 0} | ||
| */ | ||
| for(auto& i : nums) { | ||
| sorted[ counting[i]-1 ] = i; | ||
| counting[i]--; | ||
| } | ||
| | ||
| return sorted; | ||
| } | ||
| | ||
| void print_vector(vector<int> vec) { | ||
| | ||
| cout << "["; | ||
| for (size_t i = 0; i < vec.size(); i++) { | ||
| if(i != vec.size()-1) { | ||
| cout << vec[i] << ", "; | ||
| } | ||
| else { | ||
| cout << vec[i]; | ||
| } | ||
| } | ||
| cout << "]" << endl; | ||
| } | ||
| | ||
| int main() { | ||
| | ||
| vector<int> nums{5, 0, 1, 2, 3, 5, 3, 0, 9, 4}; | ||
| | ||
| vector<int> sorted = counting_sort(nums); | ||
| | ||
| cout << "Original vector = "; | ||
| print_vector(nums); | ||
| | ||
| cout << "Sorted vector = "; | ||
| print_vector(sorted); | ||
| | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #include <iostream> | ||
kelvins marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| using namespace std; | ||
| | ||
| struct Node{ | ||
| | ||
| int val; | ||
| Node* next; | ||
| | ||
| Node(int val) : val(val), next(nullptr) {} | ||
| Node(int val, Node *next) : val(val), next(next) {} | ||
| }; | ||
| | ||
| void print_list(Node *head) { | ||
| | ||
| auto aux = head; | ||
| while(aux) { | ||
| | ||
| cout << aux->val; | ||
| | ||
| if(aux->next) { | ||
| cout << " -> "; | ||
| } | ||
| | ||
| aux = aux->next; | ||
| } | ||
| cout << endl; | ||
| } | ||
| | ||
| void push_back(Node **head, int val) { | ||
| | ||
| | ||
| Node *newNode = new Node(val); | ||
| | ||
| if(*head == nullptr) { | ||
| *head = newNode; | ||
| } | ||
| | ||
| else { | ||
| | ||
| auto aux = *head; | ||
| | ||
| while(aux->next) { | ||
| aux = aux->next; | ||
| } | ||
| | ||
| aux->next = newNode; | ||
| } | ||
| } | ||
| | ||
| void push_front(Node **head, int val) { | ||
kelvins marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| | ||
| Node *newNode = new Node(val); | ||
| | ||
| if(*head == nullptr) { | ||
| *head = newNode; | ||
| } | ||
| | ||
| else { | ||
| | ||
| auto aux = *head; | ||
| | ||
| while(aux->next) { | ||
| aux = aux->next; | ||
| } | ||
| | ||
| aux->next = newNode; | ||
| } | ||
| } | ||
| | ||
| int main() { | ||
| | ||
| Node *head = nullptr; | ||
| | ||
| | ||
| push_back(&head, 1); | ||
| push_back(&head, 2); | ||
| push_back(&head, 3); | ||
| push_back(&head, 4); | ||
| push_back(&head, 5); | ||
| print_list(head); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| | ||
| | ||
| int max_recursivo(std::vector<int> nums, int n) { | ||
| | ||
| if(n == 1) | ||
| return nums[0]; | ||
| else{ | ||
| int aux = max_recursivo(nums, n-1); | ||
| | ||
| if(aux > nums[n-1]) | ||
| return aux; | ||
| | ||
| return nums[n-1]; | ||
| } | ||
| } | ||
| | ||
| int main() { | ||
| | ||
| std::vector<int> nums{1, 2, 3, 4, 32, 6, 7, 8, 9, 10}; | ||
| | ||
| std::cout << "nums = {"; | ||
| for(auto& i : nums) { | ||
| | ||
| std::cout << i; | ||
| | ||
| if(&i != &nums.back()) { | ||
| std::cout << ","; | ||
| } | ||
| } | ||
| std::cout << "}" << std::endl; | ||
| | ||
| std::cout << "Max = " << max_recursivo(nums, nums.size()) << std::endl; | ||
| | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #include <iostream> | ||
| | ||
| using namespace std; | ||
| | ||
| int main() { | ||
| | ||
| int arr[] = {1, 5, -20, 6, 15}; | ||
| | ||
| int min = arr[0]; | ||
| int max = arr[0]; | ||
| | ||
| for(auto& i : arr) { | ||
| if(i < min) min = i; | ||
| else if(i > max) max = i; | ||
| } | ||
| | ||
| cout << "Max = " + to_string(max) + "\nMin = " + to_string(min) << endl; | ||
| | ||
| return 0; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.