Skip to content
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

Create mergesort.c #1616

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
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
86 changes: 86 additions & 0 deletions Graphs/graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int **matrix;
int numEdge;
int *Mark;
}G;
G create_graph(int n) {
G grafoAux;
grafoAux.Mark = (int*) malloc(n*sizeof(int));
grafoAux.matrix = (int**) malloc(n*sizeof(int*));
for(int i=0; i<n; i++){
grafoAux.matrix[i] = (int*) malloc(n*sizeof(int));
}
grafoAux.numEdge = 0;
return grafoAux;
}
int e(G grafo){
return grafo.numEdge;
}
int first(G grafo, int v, int n){
for(int i=0; i<n; i++){
if(grafo.matrix[v][i]!=0) return i;
}
return n;
}
int next(G grafo, int v, int w, int n){
for(int i=w+1; i<n; i++){
if(grafo.matrix[v][i]!=0) return i;
}
return n;
}
void setEdge(G grafo, int i, int j, int wt){
if (wt==0) printf("ERROR\n");
if(grafo.matrix[i][j]==0) grafo.numEdge++;
grafo.matrix[i][j] = wt;
}
void delEdge(G grafo, int i, int j){
if(grafo.matrix[i][j]!=0) grafo.numEdge--;
grafo.matrix[i][j] = 0;
}
int isEdge(G grafo, int i, int j){
return (grafo.matrix[i][j]!=0);
}
int weight(G grafo, int i, int j){
return (grafo.matrix[i][j]);
}
void setMark(G grafo, int v, int val){
grafo.Mark[v] = val;
}
int getMark(G grafo, int v){
return grafo.Mark[v];
}
void print_matrix(G grafo, int n){
printf(" ");
for(int i=0; i<n; i++){
printf("%d ", i);
}
printf("\n");
for(int i=0; i<n; i++){
printf("%d ", i);
for(int j=0; j<n; j++){
printf("%d ", grafo.matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
G grafinho;
int n;
printf("insira o numero de vertices:\n");
scanf("%d", &n);
grafinho = create_graph(n);
printf("insira o numero de arestas:\n");
int e;
scanf("%d", &e);
while(e--){
printf("aresta e peso:\n");
int a, b, p;
scanf("%d %d %d", &a, &b, &p);
setEdge(grafinho,a, b, p);
}
//print_matrix(grafinho, n);

return 0;
}
56 changes: 56 additions & 0 deletions sort/mergesort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void merge(int a[], int inicio, int meio, int fim){
int *temp, p1, p2, tamanho, i, j, k;
int fim1 = 0, fim2 =0;
tamanho = fim-inicio+1;
p1 = inicio;
p2 = meio+1;
temp = (int*) malloc(tamanho*sizeof(int));
if(temp!=NULL){
for(i=0; i<tamanho; i++){
if(!fim1 && !fim2){
if(a[p1]<a[p2]){
temp[i]=a[p1++];
}
else{
temp[i]=a[p2++];
}
if(p1>meio) fim1=1;
if(p2>fim) fim2=1;
}else{
if(!fim1){
temp[i]=a[p1++];
}else{
temp[i]=a[p2++];
}
}
}
for(j=0, k=inicio; j<tamanho; j++, k++){
a[k]=temp[j];
}
}
}
void mergeSort(int a[], int inicio, int fim){
int meio;
if(inicio<fim){
meio = floor((inicio+fim)/2);
mergeSort(a, inicio, meio);
mergeSort(a, meio+1, fim);
merge(a, inicio, meio, fim);
}
}
int main(){

int vet[10000], n;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &vet[i]);
}
mergeSort(vet, 0, n-1);
for(int i=0; i<n; i++){
printf("%d ", vet[i]);
}
return 0;
}
50 changes: 50 additions & 0 deletions sort/quicksort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int partition(long long int v[], int l, int r){
long long int p = v[l];
int i = l;
int j = r+1;
long long int swap;

do{
do{
i++;
}while(!(v[i]>=p||i>=r));
do{
j--;
}while(v[j]>p);
swap = v[i];
v[i] = v[j];
v[j] = swap;
}while(i<j);
swap = v[i];
v[i] = v[j];
v[j] = swap;

swap = v[l];
v[l] = v[j];
v[j] = swap;
return j;
}
void quicksort(long long int v[], int l, int r){
int s;
if(l<r){
s = partition(v, l, r);
quicksort(v, l, s-1);
quicksort(v, s+1, r);
}
}
int main(){

long long int vet[10000], n;
scanf("%lld", &n);
for(int i=0; i<n; i++){
scanf("%lld", &vet[i]);
}
quicksort(vet, 0, n-1);
for(int i=0; i<n; i++){
printf("%lld ", vet[i]);
}
return 0;
}
20 changes: 20 additions & 0 deletions sort/toposort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define n 7
void toposort(vector<vector<int>> grafo, int *visitados, stack<int> &pilha, int no){
visitados[no] = 1;
for(auto w: grafo[no]){
if(visitados[w]==0){
toposort(grafo, visitados, pilha, w);
}
pilha.push(no);
}
}
int main()
{
vector<vector<int>> grafo(n);
stack<int> pilha;
int visitados[n]={};
return 0;
}