├── Graph ├── BFS │ ├── BFS.cpp │ └── image.jpeg ├── BellmanFord │ ├── BellmanFord.cpp │ └── image.jpeg ├── DFS │ ├── DFS.cpp │ └── image.jpeg ├── KruskalMST │ ├── Image.jpeg │ ├── KruskalMST.cpp │ ├── Makefile │ └── a.out ├── PrimsMST │ ├── Image.png │ ├── Makefile │ ├── PrimMST.cpp │ └── a.out └── dijkstra │ └── dijkstra.cpp ├── README.md └── SortingAlgorithms ├── BubbleSort ├── Makefile ├── a.cpp └── a.out ├── CountingSort ├── Makefile ├── a.cpp └── a.out ├── HeapSort ├── Makefile ├── a.cpp └── a.out ├── InsertionSort ├── Makefile ├── a.cpp └── a.out ├── MergeSort ├── Makefile ├── a.cpp └── a.out ├── QuickSort ├── Makefile ├── a.cpp └── a.out ├── RadixSort ├── Makefile ├── a.cpp └── a.out ├── SelectionSort ├── Makefile ├── a.cpp └── a.out └── ShellSort ├── Makefile ├── a.cpp └── a.out /Graph/BFS/BFS.cpp: -------------------------------------------------------------------------------- 1 | // bagli liste yapisi ile graph larda BFS dolanma kodu 2 | // Ahmet Furkan DEMIR 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // node sayisi 10 | #define count 5 11 | 12 | // komsulari tutan yapi 13 | typedef struct queue{ 14 | 15 | int val; 16 | struct queue *next; 17 | 18 | }queue; 19 | 20 | // nodelerin hepsini tutan yapi 21 | typedef struct Node{ 22 | 23 | struct queue *list[count]; 24 | 25 | }node; 26 | 27 | // ana yapiya erisilen yer 28 | node * root; 29 | 30 | // root init 31 | void __init__(){ 32 | 33 | root=(node *)malloc(sizeof(node)); 34 | } 35 | 36 | // dugumleri birbirine bagladigimiz yer 37 | // v den w ye baglanti gerceklesir 38 | void addEdge(int v, int w){ 39 | 40 | // ilk komsu eklenir 41 | if(root->list[v]==NULL){ 42 | root->list[v]=(queue *)malloc(sizeof(queue)); 43 | root->list[v]->val=w; 44 | root->list[v]->next=NULL; 45 | } 46 | 47 | // diger komsular eklenir 48 | else{ 49 | 50 | queue *temp=root->list[v]; 51 | 52 | while(temp->next!=NULL){ 53 | temp=temp->next; 54 | } 55 | 56 | temp->next=(queue *)malloc(sizeof(queue)); 57 | temp->next->val=w; 58 | temp->next->next=NULL; 59 | 60 | } 61 | 62 | } 63 | 64 | // BFS dolasma 65 | void BFS(int s){ 66 | 67 | // tum komsular gidildimi diye boolean liste 68 | bool *visited = new bool[count]; 69 | for(int i = 0; i < count; i++) 70 | visited[i] = false; 71 | 72 | // dolasma listemiz 73 | list queuea; 74 | 75 | // baslangic gidildi olarak isaretlendi 76 | visited[s] = true; 77 | // dolasma listemize eklendi 78 | queuea.push_back(s); 79 | 80 | 81 | while(!queuea.empty()) 82 | { 83 | // ekrana yazdirilip dolasma listesinden cikartilir 84 | s = queuea.front(); 85 | printf("%d ",s); 86 | queuea.pop_front(); 87 | 88 | // secili node 'ın tum komsularina gidilir 89 | queue *temp= root->list[s]; 90 | while(temp!=NULL){ 91 | 92 | // bu komsuya daha once girilmediyse iceri girilir 93 | if (!visited[temp->val]) 94 | { 95 | // komsu gidildi olarak isaretlenir ve listeye eklenir 96 | visited[temp->val] = true; 97 | queuea.push_back(temp->val); 98 | } 99 | 100 | // sonraki komsuya gecilir. 101 | temp=temp->next; 102 | 103 | } 104 | 105 | } 106 | 107 | } 108 | 109 | // main 110 | int main() 111 | { 112 | 113 | // init ve ekleme islemi 114 | __init__(); 115 | addEdge(0, 1); 116 | addEdge(0, 3); 117 | addEdge(0, 2); 118 | addEdge(1, 3); 119 | addEdge(1, 2); 120 | addEdge(2, 4); 121 | addEdge(3, 4); 122 | addEdge(3, 2); 123 | addEdge(4, 1); 124 | 125 | // BFS dolasma, root olarak 0 secildi. 126 | BFS(0); 127 | 128 | 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /Graph/BFS/image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/BFS/image.jpeg -------------------------------------------------------------------------------- /Graph/BellmanFord/BellmanFord.cpp: -------------------------------------------------------------------------------- 1 | // bagli liste yapisi ile graph larda belman-ford ile en kisa yol bulma algoritmasi 2 | // Ahmet Furkan DEMIR 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // node sayisi (vertex) 10 | #define count 6 11 | 12 | // komsulari tutan yapi (komsu vertexler) 13 | typedef struct queue{ 14 | 15 | int id; 16 | int weight; 17 | struct queue *next; 18 | 19 | }queue; 20 | 21 | // nodelerin hepsini tutan yapi 22 | typedef struct Node{ 23 | 24 | // list yapisi vertexlerin hepsini tutacak 25 | struct queue *list[count]; 26 | 27 | }node; 28 | 29 | // ana yapiya erisilen yer 30 | node * root; 31 | 32 | // root init 33 | void __init__(){ 34 | 35 | root=(node *)malloc(sizeof(node)); 36 | } 37 | 38 | // dugumleri (vertexleri) birbirine bagladigimiz yer, edge ler olusturdugumuz yer 39 | // v den w ye baglanti gerceklesir, agirliklar (weight) ise komsular arasi mesafe 40 | void addEdge(int v, int w, int value){ 41 | 42 | // ilk komsu eklenir 43 | if(root->list[v]==NULL){ 44 | root->list[v]=(queue *)malloc(sizeof(queue)); 45 | root->list[v]->weight=value; 46 | root->list[v]->id=w; 47 | root->list[v]->next=NULL; 48 | } 49 | 50 | // diger komsular eklenir 51 | else{ 52 | 53 | queue *temp=root->list[v]; 54 | 55 | while(temp->next!=NULL){ 56 | temp=temp->next; 57 | } 58 | 59 | temp->next=(queue *)malloc(sizeof(queue)); 60 | temp->next->weight=value; 61 | temp->next->id=w; 62 | temp->next->next=NULL; 63 | 64 | } 65 | 66 | } 67 | 68 | // dist dizisi ekrana print edilir 69 | // bu dizi ana vertexe olan uzaklıkları tutar 70 | void print(int dist[]) 71 | { 72 | printf("Vertex Shortest distance\n"); 73 | for (int i = 0; i < count; ++i){ 74 | printf("%d \t\t %d\n", i, dist[i]); 75 | } 76 | } 77 | 78 | // belman-ford algoritmasi 79 | void BellmanFord(int s) { 80 | 81 | // ana vertexe uzakligi tutacak olan dizi 82 | int dist[count]; 83 | 84 | // max deger atiyoruz, cunki kucukmu diye karsilastirip yeni deger atamasi yapacagiz 85 | for (int i = 0; i < count; i++){ 86 | 87 | dist[i] = 999; 88 | } 89 | 90 | // baslangic vertex = 0 91 | dist[s] = 0; 92 | 93 | // tum vertexlerimize sırayla ilerleyecegiz 94 | for (int i = 0; i < count; i++) { 95 | 96 | // verteximizin komsularini dolasacagiz 97 | queue *temp= root->list[i]; 98 | while(temp!=NULL){ 99 | 100 | // u = suanki vertex 101 | int u = i; 102 | 103 | // hedef (komsu) vertex 104 | int v = temp->id; 105 | 106 | // uzaklik yani weight 107 | int weight = temp->weight; 108 | 109 | // eger kucuk ise mevcut dist guncellenir ve yeni en kısa mesefa bu guncelleme olur 110 | // eger buyuk ise herhangi bir guncellemeye gerek kalmaz. 111 | if (dist[u] != 999 && dist[u] + weight < dist[v]){ 112 | dist[v] = dist[u] + weight; 113 | } 114 | 115 | // sonraki komsu vertexe gecilir 116 | temp=temp->next; 117 | } 118 | 119 | } 120 | 121 | // mesefe print edilir 122 | print(dist); 123 | 124 | } 125 | 126 | 127 | // main 128 | int main() 129 | { 130 | 131 | // init ve ekleme islemi 132 | __init__(); 133 | addEdge(0, 1, 8); 134 | addEdge(0, 2, 1); 135 | 136 | addEdge(1, 5, -3); 137 | addEdge(1, 3, 4); 138 | 139 | addEdge(2, 3, 2); 140 | addEdge(2, 4, 5); 141 | 142 | addEdge(3, 2, 8); 143 | addEdge(3, 4, 6); 144 | 145 | addEdge(4, 3, 1); 146 | addEdge(4, 5, 3); 147 | addEdge(4, 1, -6); 148 | addEdge(5, 4, -2); 149 | 150 | // algoritma 151 | BellmanFord(0); 152 | 153 | 154 | return 0; 155 | } 156 | -------------------------------------------------------------------------------- /Graph/BellmanFord/image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/BellmanFord/image.jpeg -------------------------------------------------------------------------------- /Graph/DFS/DFS.cpp: -------------------------------------------------------------------------------- 1 | // bagli liste yapisi ile graph larda DFS dolanma kodu 2 | // Ahmet Furkan DEMIR 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // node sayisi 10 | #define count 6 11 | 12 | // komsulari tutan yapi 13 | typedef struct queue{ 14 | 15 | int val; 16 | struct queue *next; 17 | 18 | }queue; 19 | 20 | // nodelerin hepsini tutan yapi 21 | typedef struct Node{ 22 | 23 | struct queue *list[count]; 24 | 25 | }node; 26 | 27 | // ana yapiya erisilen yer 28 | node * root; 29 | 30 | // root init 31 | void __init__(){ 32 | 33 | root=(node *)malloc(sizeof(node)); 34 | } 35 | 36 | // dugumleri birbirine bagladigimiz yer 37 | // v den w ye baglanti gerceklesir 38 | void addEdge(int v, int w){ 39 | 40 | // ilk komsu eklenir 41 | if(root->list[v]==NULL){ 42 | root->list[v]=(queue *)malloc(sizeof(queue)); 43 | root->list[v]->val=w; 44 | root->list[v]->next=NULL; 45 | } 46 | 47 | // diger komsular eklenir 48 | else{ 49 | 50 | queue *temp=root->list[v]; 51 | 52 | while(temp->next!=NULL){ 53 | temp=temp->next; 54 | } 55 | 56 | temp->next=(queue *)malloc(sizeof(queue)); 57 | temp->next->val=w; 58 | temp->next->next=NULL; 59 | 60 | } 61 | 62 | } 63 | 64 | void DFS_recursiv(int v, bool visited[]){ 65 | 66 | // dugum yazdirilir ve gidildi olarak isaretlenir 67 | visited[v] = true; 68 | printf("%d ", v); 69 | 70 | // dugumun tum komsulari 71 | // en sona kadar ilerler ekrana yazdirir ve komsular bitince diger taraftan devam eder. 72 | queue *temp= root->list[v]; 73 | while(temp!=NULL){ 74 | 75 | if (!visited[temp->val]) 76 | 77 | // bu komsuya daha once girilmediyse iceri girilir ve kod recursiv olarak devam eder 78 | DFS_recursiv(temp->val, visited); 79 | 80 | // sonraki komsuya gecilir. 81 | temp=temp->next; 82 | 83 | } 84 | } 85 | 86 | // DFS dolasma 87 | void DFS(int s){ 88 | 89 | // tum komsular gidildimi diye boolean liste 90 | bool* visited = new bool[count]; 91 | for (int i = 0; i < count; i++) 92 | visited[i] = false; 93 | 94 | // recursiv fonksiyon 95 | // graphı DFS olarak dolasacak 96 | DFS_recursiv(s, visited); 97 | 98 | } 99 | 100 | // main 101 | int main() 102 | { 103 | 104 | // init ve ekleme islemi 105 | __init__(); 106 | addEdge(0, 1); 107 | addEdge(0, 3); 108 | addEdge(1, 2); 109 | addEdge(3, 5); 110 | addEdge(5, 0); 111 | addEdge(5, 3); 112 | addEdge(5, 4); 113 | addEdge(2, 3); 114 | addEdge(2, 4); 115 | addEdge(4, 2); 116 | 117 | // DFS dolasma, root olarak 0 secildi. 118 | DFS(0); 119 | 120 | 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /Graph/DFS/image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/DFS/image.jpeg -------------------------------------------------------------------------------- /Graph/KruskalMST/Image.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/KruskalMST/Image.jpeg -------------------------------------------------------------------------------- /Graph/KruskalMST/KruskalMST.cpp: -------------------------------------------------------------------------------- 1 | // bagli liste yapisi ile Kruksal's Minimum Spanning Tree 2 | // Ahmet Furkan DEMIR 3 | 4 | #include 5 | #include 6 | 7 | // toplam vertex sayisi 8 | #define vertex 9 9 | 10 | // Birleşim bulma için bir alt kümeyi temsil eden bir yapı 11 | // cycle olusuyormu kontrolu 12 | struct subset { 13 | int parent; 14 | int rank; 15 | }; 16 | 17 | // komsulari tutan yapi (komsu vertexler) 18 | typedef struct Edge{ 19 | 20 | // mevcut vertex 21 | int src; 22 | 23 | // hedef vertex 24 | int dest; 25 | 26 | // agirlik, mesafe 27 | int weight; 28 | 29 | // sonraki vertex 30 | struct Edge *next; 31 | 32 | }edge; 33 | 34 | // ana yapi ve kucukten buyuge siralanmis olan yapi. 35 | edge * root, * small; 36 | 37 | // root init 38 | void __init__(){ 39 | 40 | root=(edge *)malloc(sizeof(edge)); 41 | small=(edge *)malloc(sizeof(edge)); 42 | } 43 | 44 | // vertexleri birbirine bagladigimiz yer, edge ler olusturdugumuz yer 45 | // v den w ye baglanti gerceklesir, agirliklar (weight) ise komsular arasi mesafe 46 | void addEdge(int v, int w, int value){ 47 | 48 | // ilk edge eklenir 49 | if(root==NULL){ 50 | root=(edge *)malloc(sizeof(edge)); 51 | root->weight=value; 52 | root->dest=w; 53 | root->src=v; 54 | root->next=NULL; 55 | } 56 | 57 | // diger komsular eklenir 58 | else{ 59 | 60 | edge * temp =root; 61 | 62 | while(temp->next!=NULL){ 63 | temp=temp->next; 64 | } 65 | 66 | temp->next=(edge *)malloc(sizeof(edge)); 67 | temp->next->weight=value; 68 | temp->next->dest=w; 69 | temp->next->src=v; 70 | temp->next->next=NULL; 71 | 72 | } 73 | 74 | } 75 | 76 | // ana yapiyi kucukten buyuge siraladigimiz yer 77 | void sort_small(){ 78 | 79 | int count, flag; 80 | 81 | edge * temp = root; 82 | 83 | while(temp!=NULL){ 84 | 85 | flag=0; 86 | 87 | // ilk deger eklenir 88 | if(small==NULL){ 89 | small->src=temp->src; 90 | small->dest=temp->dest; 91 | small->weight=temp->weight; 92 | small->next=NULL; 93 | } 94 | 95 | // sonraki vertexler 96 | else{ 97 | 98 | // eger small yapisindaki degerden daha buyuk ise yeni root degeri atanir ve yapi bir arkaya kayar. 99 | if(small->weight>temp->weight){ 100 | 101 | edge * temp_small=(edge *)malloc(sizeof(edge)); 102 | temp_small->src=temp->src; 103 | temp_small->dest=temp->dest; 104 | temp_small->weight=temp->weight; 105 | temp_small->next=small; 106 | small=temp_small; 107 | 108 | 109 | } 110 | 111 | // ortadan veya sondan siralamaya gore ekleme 112 | else{ 113 | 114 | edge *temp_edge = small; 115 | 116 | while(temp_edge->next!=NULL){ 117 | 118 | // ortadan ekleme 119 | if(temp_edge->next->weight>temp->weight){ 120 | 121 | edge * temp_small=(edge *)malloc(sizeof(edge)); 122 | temp_small->src=temp->src; 123 | temp_small->dest=temp->dest; 124 | temp_small->weight=temp->weight; 125 | temp_small->next=small; 126 | temp_small->next=temp_edge->next; 127 | temp_edge->next=temp_small; 128 | 129 | flag=1; 130 | 131 | break; 132 | 133 | } 134 | 135 | temp_edge=temp_edge->next; 136 | } 137 | 138 | // sondan ekleme 139 | if(flag==0){ 140 | temp_edge->next=(edge *)malloc(sizeof(edge)); 141 | temp_edge->next->src=temp->src; 142 | temp_edge->next->dest=temp->dest; 143 | temp_edge->next->weight=temp->weight; 144 | temp_edge->next->next=NULL; 145 | } 146 | 147 | } 148 | 149 | } 150 | 151 | // sonraki vertex 152 | temp=temp->next; 153 | 154 | } 155 | } 156 | 157 | 158 | // cycle kontrolu icin yardimci islev 159 | int find(struct subset subsets[], int i) 160 | { 161 | if (subsets[i].parent != i) 162 | subsets[i].parent = find(subsets, subsets[i].parent); 163 | 164 | // return parent 165 | return subsets[i].parent; 166 | } 167 | 168 | // x ve y 'yi birlestirme fonksiyonu 169 | void Union(struct subset subsets[], int x, int y) 170 | { 171 | 172 | int xroot = find(subsets, x); 173 | int yroot = find(subsets, y); 174 | 175 | // sirali olarak birlestirme, kucuklerden. 176 | if (subsets[xroot].rank < subsets[yroot].rank) 177 | subsets[xroot].parent = yroot; 178 | else if (subsets[xroot].rank > subsets[yroot].rank) 179 | subsets[yroot].parent = xroot; 180 | 181 | // Sıralar aynıysa 182 | else 183 | { 184 | subsets[yroot].parent = xroot; 185 | subsets[xroot].rank++; 186 | } 187 | } 188 | 189 | // algoriitma 190 | void KruskalMST() 191 | { 192 | edge result[vertex]; // sonuc yapimiz 193 | int e = 0; // result dizisini indisliyen degisken 194 | int count; 195 | 196 | // vertexin alt kumeleri icin 197 | struct subset* subsets = (struct subset*)malloc(vertex * sizeof(struct subset)); 198 | 199 | for (int v = 0; v < vertex; ++v) { 200 | subsets[v].parent = v; 201 | subsets[v].rank = 0; 202 | } 203 | 204 | // yapiyi (small) dolasip islem yaptigimiz yer, son vertex e kadar ilerlenir. 205 | while(small!=NULL){ 206 | 207 | // yapi da bir cycle olusuyormu diye kontrol edecegiz. 208 | int x = find(subsets, small->src); 209 | int y = find(subsets, small->dest); 210 | 211 | // cycle kontrolu 212 | if (x != y) { 213 | result[e++] = * small; 214 | 215 | // birlestirme islemi 216 | Union(subsets, x, y); 217 | } 218 | 219 | // sonraki edge 220 | small=small->next; 221 | } 222 | 223 | // yapimizin son hali ve minCost 'u ekrana yazdirdik. 224 | int minimumCost = 0, i; 225 | for (i = 0; i < e; ++i) 226 | { 227 | printf("%d -> %d == %d\n", result[i].src, result[i].dest, result[i].weight); 228 | minimumCost += result[i].weight; 229 | } 230 | printf("Minimum Cost : %d",minimumCost); 231 | return; 232 | } 233 | 234 | int main(){ 235 | 236 | // init ve ekleme islemi 237 | __init__(); 238 | addEdge(7, 6, 1); 239 | addEdge(8, 2, 2); 240 | addEdge(6, 5, 2); 241 | addEdge(0, 1, 4); 242 | addEdge(2, 5, 4); 243 | addEdge(8, 6, 6); 244 | addEdge(2, 3, 7); 245 | addEdge(7, 8, 7); 246 | addEdge(0, 7, 8); 247 | addEdge(1, 2, 8); 248 | addEdge(3, 4, 9); 249 | addEdge(5, 4, 10); 250 | addEdge(1, 7, 11); 251 | addEdge(3, 5, 14); 252 | 253 | // yapiyi (vertexleri), weight degerine gore kucukten buyuge siralama islemi 254 | sort_small(); 255 | 256 | printf("\n"); 257 | 258 | // Kruskal algoritmasi 259 | KruskalMST(); 260 | 261 | printf("\n"); 262 | 263 | return 0; 264 | } 265 | -------------------------------------------------------------------------------- /Graph/KruskalMST/Makefile: -------------------------------------------------------------------------------- 1 | run: KruskalMST.cpp 2 | clear 3 | gcc KruskalMST.cpp 4 | ./a.out -------------------------------------------------------------------------------- /Graph/KruskalMST/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/KruskalMST/a.out -------------------------------------------------------------------------------- /Graph/PrimsMST/Image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/PrimsMST/Image.png -------------------------------------------------------------------------------- /Graph/PrimsMST/Makefile: -------------------------------------------------------------------------------- 1 | run: PrimMST.cpp 2 | clear 3 | gcc PrimMST.cpp 4 | ./a.out 5 | -------------------------------------------------------------------------------- /Graph/PrimsMST/PrimMST.cpp: -------------------------------------------------------------------------------- 1 | // Ahmet Furkan DEMIR 2 | // prims algorithm 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // vertex sayisi 10 | #define vertex 10 11 | 12 | // komsulari tutan yapi (edge) (komsu vertexler) 13 | typedef struct edge{ 14 | 15 | int dest; 16 | int weight; 17 | struct edge *next; 18 | 19 | }edge; 20 | 21 | // vertexlerin hepsini tutan yapi 22 | typedef struct Node{ 23 | 24 | // list yapisi vertexlerin hepsini tutacak 25 | struct edge *list[vertex]; 26 | 27 | }node; 28 | 29 | // ana yapiya erisilen yer 30 | node * root; 31 | 32 | // root init 33 | void __init__(){ 34 | 35 | root=(node *)malloc(sizeof(node)); 36 | } 37 | 38 | // dugumleri (vertexleri) birbirine bagladigimiz yer, edge ler olusturdugumuz yer 39 | // v den w ye baglanti gerceklesir, agirliklar (weight) ise komsular arasi mesafe 40 | void addEdge(int v, int w, int value){ 41 | 42 | // ilk komsu eklenir 43 | if(root->list[v]==NULL){ 44 | root->list[v]=(edge *)malloc(sizeof(edge)); 45 | root->list[v]->weight=value; 46 | root->list[v]->dest=w; 47 | root->list[v]->next=NULL; 48 | } 49 | 50 | // diger komsular eklenir 51 | else{ 52 | 53 | edge *temp=root->list[v]; 54 | 55 | while(temp->next!=NULL){ 56 | temp=temp->next; 57 | } 58 | 59 | temp->next=(edge *)malloc(sizeof(edge)); 60 | temp->next->weight=value; 61 | temp->next->dest=w; 62 | temp->next->next=NULL; 63 | 64 | } 65 | 66 | } 67 | 68 | // yonsuz baglanti gerceklestirilir, boylece baglanti cift yonlu olur 69 | void addEdge0(int v, int w, int value){ 70 | 71 | addEdge(v, w, value); 72 | addEdge(w, v, value); 73 | 74 | } 75 | 76 | // komsu vertexlerinden minimum weight 'ın adresini dondurur. 77 | int minKey(int key[], bool mstSet[]) 78 | { 79 | int min = 999, min_index; 80 | 81 | for (int v = 0; v < vertex; v++){ 82 | if (mstSet[v] == false && key[v] < min){ 83 | min = key[v], min_index = v; 84 | } 85 | } 86 | 87 | return min_index; 88 | } 89 | 90 | // primis mst ile olusturulan yeni graph print edilir. 91 | int printMST(int parent[]){ 92 | printf("Edge \tWeight\n"); 93 | 94 | // her grapha zorunlu olarak gidilecegi icin, gidilecek yerleri bu degiskenden alacagiz. 95 | for (int i = 1; i < vertex; i++){ 96 | 97 | // vertexin komsulari dolasilir 98 | edge * temp = root->list[parent[i]]; 99 | 100 | while(temp!=NULL){ 101 | 102 | // i vertexine gidilen weight yazdirilir. 103 | if(temp->dest==i){ 104 | printf("%d - %d -> %d\n", parent[i], i, temp->weight); 105 | break; 106 | } 107 | 108 | temp=temp->next; 109 | } 110 | 111 | } 112 | 113 | return 0; 114 | } 115 | 116 | // primMST 117 | void primMST() 118 | { 119 | // Oluşturulan MST'yi depolamak için dizi 120 | int parent[vertex]; 121 | 122 | // minimum vertexler (komsular) 123 | int key[vertex]; 124 | 125 | // gidilen yerleri isaretleyip, birdaha gidilmemesini saglamak 126 | bool mstSet[vertex]; 127 | 128 | // degerleri baslatiyoruz 129 | for (int i = 0; i < vertex; i++){ 130 | 131 | key[i] = 999; 132 | mstSet[i] = false; 133 | 134 | } 135 | 136 | // baslangic vertexi 137 | key[0] = 0; 138 | parent[0] = -1; // First node is always root of MST 139 | 140 | // Tüm vertexleri dolasir 141 | for (int count = 0; count < vertex; count++) { 142 | // min vertex 143 | int u = minKey(key, mstSet); 144 | 145 | // vertexi gidildi olarak isaretliyoruz 146 | mstSet[u] = true; 147 | 148 | edge * temp = root->list[u]; 149 | while(temp!=NULL){ 150 | 151 | if (temp->weight && mstSet[temp->dest] == false && temp->weight < key[temp->dest]){ 152 | 153 | parent[temp->dest] = u, key[temp->dest] = temp->weight; 154 | } 155 | 156 | temp=temp->next; 157 | } 158 | 159 | } 160 | // print primis algoritmasi 161 | printMST(parent); 162 | } 163 | 164 | 165 | int main(){ 166 | 167 | 168 | // init ve ekleme islemi 169 | __init__(); 170 | addEdge0(0, 1, 10); 171 | addEdge0(1, 2, 8); 172 | addEdge0(2, 3, 13); 173 | addEdge0(4, 3, 26); 174 | addEdge0(1, 4, 30); 175 | addEdge0(2, 4, 38); 176 | addEdge0(3, 5, 23); 177 | addEdge0(5, 8, 14); 178 | addEdge0(8, 7, 3); 179 | addEdge0(5, 7, 19); 180 | addEdge0(6, 7, 32); 181 | addEdge0(5, 6, 48); 182 | addEdge0(7, 9, 36); 183 | 184 | primMST(); 185 | 186 | 187 | return 0; 188 | } 189 | -------------------------------------------------------------------------------- /Graph/PrimsMST/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/Graph/PrimsMST/a.out -------------------------------------------------------------------------------- /Graph/dijkstra/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Vertex sayisi 5 | #define DUGUM_SAYISI 5 6 | 7 | int GRAF[DUGUM_SAYISI][DUGUM_SAYISI]; 8 | 9 | void __init__(){ 10 | 11 | // aray 0 lardan olusturuluyor, eger o vertexe baglanti yok ise herhangi bie ekleme islemine gerek yok. 12 | GRAF[DUGUM_SAYISI][DUGUM_SAYISI] = {0}; 13 | 14 | 15 | } 16 | 17 | void add(int src, int dest, int weight){ 18 | 19 | if(weight<0){ 20 | printf("Hata!!, Dijkstra algoritmasında agirliklar eksi (-) deger alamaz !!.\n"); 21 | } 22 | 23 | GRAF[src][dest] = weight; 24 | 25 | } 26 | 27 | 28 | // Minimum mesafe değerine sahip tepe noktasını bulmak için yazılmış fonksiyon 29 | int minDistance(int dist[], bool sptSet[]) 30 | { 31 | // min degeri bulmak icin max deger atiyoruz 32 | int min = 999, min_index; 33 | 34 | for (int v = 0; v < DUGUM_SAYISI; v++) 35 | if (sptSet[v] == false && dist[v] <= min) 36 | min = dist[v], min_index = v; 37 | 38 | return min_index; 39 | } 40 | 41 | // mesafe dizisi yazdırılır 42 | void printSolution(int dist[]) 43 | { 44 | printf("Vertex Kaynaga olan uzaklik \n"); 45 | for (int i = 0; i < DUGUM_SAYISI; i++) 46 | printf("%d %d\n", i, dist[i]); 47 | } 48 | 49 | // matris kullanarak djikstra algoritmasi ile hedef vertexlere min degerleri bulma fonksiyonu 50 | void dijkstra(int src) 51 | { 52 | int dist[DUGUM_SAYISI]; // Çıktı dizisi, uzakligi tutacak 53 | 54 | bool sptSet[DUGUM_SAYISI]; // en kisa koseye gidilme konrolu 55 | 56 | // Mesafeleri sonsuz olarak baslatma ve en kisa koseye gidilme konrolu False olarak baslatma 57 | for (int i = 0; i < DUGUM_SAYISI; i++) 58 | dist[i] = 999, sptSet[i] = false; 59 | 60 | // kaynagin kendisine olan uzakligi = 0 61 | dist[src] = 0; 62 | 63 | // tum koseler icin en kisa yol aranir 64 | for (int count = 0; count < DUGUM_SAYISI - 1; count++) { 65 | 66 | int u = minDistance(dist, sptSet); // en kısa mesafeye sahip vertex fonksiyon yardimi ile secilir. 67 | 68 | // vertexe gidildi olarak isaretlenir 69 | sptSet[u] = true; 70 | 71 | // seçilen vertexin komsu vertexlerinin dist degerini guncelleme 72 | for (int v = 0; v < DUGUM_SAYISI; v++) 73 | 74 | // guncelleme 75 | if (!sptSet[v] && GRAF[u][v] && dist[u] != 999 76 | && dist[u] + GRAF[u][v] < dist[v]) 77 | dist[v] = dist[u] + GRAF[u][v]; 78 | } 79 | 80 | // uzunlugu tutan arrayin ekrana yazdirilmasi 81 | printSolution(dist); 82 | } 83 | 84 | int main() 85 | { 86 | __init__(); 87 | add(0, 1, 8); 88 | add(0, 2, 9); 89 | add(0, 3, 4); 90 | 91 | add(1, 0, 2); 92 | add(1, 3, 9); 93 | add(1, 4, 12); 94 | 95 | add(2, 0, 9); 96 | add(2, 4, 24); 97 | 98 | add(3, 0, 9); 99 | add(3, 2, 6); 100 | add(3, 3, 6); 101 | 102 | add(4, 0, 7); 103 | add(4, 1, 8); 104 | add(4, 2, 32); 105 | add(4, 3, 33); 106 | add(4, 4, 1); 107 | 108 | // dijkstra algoritma 109 | dijkstra(0); 110 | 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://img.shields.io/badge/C%2B%2B-00599C?style=for-the-badge&logo=c%2B%2B&logoColor=white) 2 | 3 | # Algorithms 4 | 5 | *-* My code repository where I share my Necmettin Erbakan University Computer Engineering 2nd year algorithms course homework, projects and exams. 6 | 7 | ### Topics 8 | 9 | * [x] [Graph](/Graph/) 10 | 11 | - [x] [BFS (Breadth First Search)](/Graph/BFS/) 12 | 13 | - [x] [DFS (Depth First Search)](/Graph/DFS/) 14 | 15 | - [x] [Dijkstra](/Graph/dijkstra/dijkstra.cpp/) 16 | 17 | - [x] [Bellman Ford](/Graph/BellmanFord/) 18 | 19 | - [x] [Kruksal's Minimum Spanning Tree](/Graph/KruskalMST/) 20 | 21 | - [x] [Prim's Minimum Spanning Tree](/Graph/PrimsMST/) 22 | 23 | 24 | * [x] [Sorting Algorithms](/SortingAlgorithms/) 25 | 26 | - [x] [Bubble Sort](/SortingAlgorithms/BubbleSort/) 27 | 28 | - [x] [Insertion Sort](/SortingAlgorithms/InsertionSort/) 29 | 30 | - [X] [Selection Sort](/SortingAlgorithms/SelectionSort/) 31 | 32 | - [X] [Shell Sort](/SortingAlgorithms/ShellSort/) 33 | 34 | - [X] [Merge Sort](/SortingAlgorithms/MergeSort/) 35 | 36 | - [X] [Quick Sort](/SortingAlgorithms/QuickSort/) 37 | 38 | - [x] [Counting Sort](/SortingAlgorithms/CountingSort/) 39 | 40 | - [x] [Radix Sort](/SortingAlgorithms/RadixSort/) 41 | 42 | - [x] [Heap Sort](/SortingAlgorithms/HeapSort/) 43 | 44 | 45 | -------------------------------------------------------------------------------- /SortingAlgorithms/BubbleSort/Makefile: -------------------------------------------------------------------------------- 1 | run: a.cpp 2 | clear 3 | gcc a.cpp 4 | ./a.out -------------------------------------------------------------------------------- /SortingAlgorithms/BubbleSort/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void BubbleSort(int array[], int size){ 5 | 6 | int index; 7 | int flag=1; 8 | 9 | while(flag){ 10 | 11 | flag=0; 12 | 13 | for(int i=0; iarray[i+1]){ 17 | 18 | flag=1; 19 | 20 | int temp = array[i]; 21 | array[i] = array[i+1]; 22 | array[i+1] = temp; 23 | 24 | } 25 | 26 | } 27 | 28 | } 29 | } 30 | 31 | void print(int array[], int size){ 32 | 33 | for(int i=0; i 2 | 3 | void CountingSort(int array[], int size){ 4 | 5 | 6 | int temp[size]={0}; 7 | 8 | for(int i=0; i 2 | 3 | void swap(int &a, int &b) { 4 | int t = a; 5 | a = b; 6 | b = t; 7 | } 8 | 9 | void heapify(int array[], int n, int i) 10 | { 11 | int largest = i; 12 | int l = 2 * i + 1; 13 | int r = 2 * i + 2; 14 | 15 | 16 | if (l < n && array[l] > array[largest]) 17 | largest = l; 18 | 19 | if (r < n && array[r] > array[largest]) 20 | largest = r; 21 | 22 | 23 | if (largest != i) { 24 | swap(array[i], array[largest]); 25 | 26 | heapify(array, n, largest); 27 | } 28 | } 29 | 30 | 31 | void HeapSort(int array[], int n) 32 | { 33 | 34 | for (int i = n / 2 - 1; i >= 0; i--){ 35 | 36 | heapify(array, n, i); 37 | 38 | } 39 | 40 | 41 | for (int i = n - 1; i > 0; i--) { 42 | 43 | swap(array[0], array[i]); 44 | heapify(array, i, 0); 45 | } 46 | } 47 | 48 | 49 | void printArray(int array[], int n) 50 | { 51 | for (int i = 0; i < n; ++i){ 52 | 53 | printf("%d ", array[i]); 54 | 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | int array[] = {5 ,8 ,9 ,45, 87, 52, 65656, 41, 2, 0, 0, 8, 9, 7, 0}; 61 | int n = sizeof(array) / sizeof(array[0]); 62 | 63 | HeapSort(array, n); 64 | printArray(array, n); 65 | } 66 | -------------------------------------------------------------------------------- /SortingAlgorithms/HeapSort/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmetFurkanDEMIR/Algorithms/cd269aa67e720d5446f837956e7be1e3be8a8956/SortingAlgorithms/HeapSort/a.out -------------------------------------------------------------------------------- /SortingAlgorithms/InsertionSort/Makefile: -------------------------------------------------------------------------------- 1 | run: a.cpp 2 | clear 3 | gcc a.cpp 4 | ./a.out -------------------------------------------------------------------------------- /SortingAlgorithms/InsertionSort/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void InsertionSort(int array[], int size){ 5 | 6 | int index; 7 | 8 | for(int i=1; i0 && array[index] 2 | #include 3 | 4 | void Merge(int array[], int l, int m, int r){ 5 | 6 | int i, j, k; 7 | 8 | int n1 = m - l + 1; 9 | int n2 = r - m; 10 | 11 | int L[n1]; 12 | int R[n2]; 13 | 14 | for(i=0; i 2 | #include 3 | 4 | void QuickSort(int array[], int l, int r){ 5 | 6 | int k = l, j = r, sayac, mid, desk; 7 | 8 | mid=array[(l+r)/2]; 9 | 10 | do{ 11 | 12 | while(mid>array[k] && k < r){ 13 | k++; 14 | } 15 | 16 | while(midl){ 17 | j--; 18 | } 19 | 20 | if(k<=j){ 21 | desk=array[k]; 22 | array[k]=array[j]; 23 | array[j]=desk; 24 | k++; 25 | j--; 26 | } 27 | 28 | }while(k<=j); 29 | 30 | if(l 2 | 3 | void RadixSort(int array[], int size){ 4 | 5 | 6 | int d=-9,temp, d_temp; 7 | 8 | for(int i=0; i0){ 15 | 16 | temp=temp/10; 17 | 18 | d_temp++; 19 | 20 | } 21 | 22 | if(d<=d_temp){ 23 | 24 | d=d_temp; 25 | 26 | } 27 | 28 | } 29 | 30 | int bs = 1; 31 | 32 | for(int i=0; i= 0; j--) { 48 | output[count[(array[j] / bs) % 10] - 1] = array[j]; 49 | count[(array[j] / bs) % 10]--; 50 | } 51 | 52 | for (j = 0; j < size; j++){ 53 | array[j] = output[j]; 54 | } 55 | 56 | bs*=10; 57 | } 58 | 59 | 60 | } 61 | 62 | void PrintArray(int array[], int size){ 63 | 64 | 65 | for(int i=0; i 2 | #include 3 | 4 | void SelectionSort(int array[], int size){ 5 | 6 | for(int i=0; iarray[j]){ 11 | 12 | int temp = array[i]; 13 | array[i] = array[j]; 14 | array[j] = temp; 15 | 16 | } 17 | 18 | } 19 | 20 | } 21 | 22 | } 23 | 24 | void print(int array[], int size){ 25 | 26 | for(int i=0; i 2 | #include 3 | 4 | void ShellSort(int array[], int size){ 5 | 6 | int Increase=size/2; 7 | int i,j, temp; 8 | 9 | while(Increase!=0){ 10 | 11 | for(i=Increase; i=0 && temp