1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct {
string tarih;
float deger;
}divide_and_conq_dt;
void merge_sort(divide_and_conq_dt[],int,int);
void merge(divide_and_conq_dt[], int , int , int );
int main()
{
divide_and_conq_dt dizi[5];
int begin = 0;
int end = 5;
for (int i = 0; i < 5; i++) {
cout << "tarih gir" << endl;
cin >> dizi[i].tarih;
cout << "deger gir" << endl;
cin >> dizi[i].deger;
}
merge_sort(dizi,begin,end);
for (int i = 0; i < 5; i++) {
cout << "tarih:" << dizi[i].tarih << "---" << "deger:" << dizi[i].deger << endl;
}
}
static void merge_sort(divide_and_conq_dt dizi[],int begin,int end) {
int middle = 0;
if (begin < end) {
middle = (begin + end) / 2;
merge_sort(dizi, begin, middle);
merge_sort(dizi, middle + 1, end);
merge(dizi,begin, middle, end);
}
}
void merge(divide_and_conq_dt dizi[], int begin, int middle, int end) {
int i = 0;
int left = begin;
int right = middle + 1;
int combined = begin;
divide_and_conq_dt temp[5];
while (left<=middle&&right<=end) {
if (dizi[left].deger <= dizi[right].deger) {
temp[combined++] = dizi[left++];
}
else
{
temp[combined++] = dizi[right++];
}
}
if (left == middle + 1) {
while (right <= end) {
temp[combined++] = dizi[right++];
}
}
else {
while (left <= middle) {
temp[combined++] = dizi[left++];
}
}
for (i = end; i <= end; i++) {
dizi[i] = temp[i];
}
}
|