• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# asdasd

matheus__serpa May 19th, 2020 1,016 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. void merge(int *A, int esq, int meio, int dir){
5.     int *v = (int *) malloc((dir - esq + 1) * sizeof(int));
6.     int i = esq;
7.     int j = meio + 1;
8.     int k = 0;
9.
10.     while(i <= meio && j <= dir){
11.         if(A[i] < A[j]){
12.             v[k] = A[i];
13.             i++;
14.         }else{
15.             v[k] = A[j];
16.             j++;
17.         }
18.         k++;
19.     }
20.
21.     while(i <= meio){
22.         v[k] = A[i];
23.         i++;
24.         k++;
25.     }
26.
27.     while(j <= dir){
28.         v[k] = A[j];
29.         j++;
30.         k++;
31.     }
32.
33.     for(i = 0; i < k; i++)
34.         A[esq + i] = v[i];
35. }
36.
37. void merge_sort(int *A, int esq, int dir){
38.     if(esq < dir){
39.         int meio = (esq + dir) / 2;
40.         merge_sort(A, esq, meio);
41.         merge_sort(A, meio + 1, dir);
42.         merge(A, esq, meio, dir);
43.     }
44. }
45.
46. int main(){
47.     int i, A[5] = {4, 7, 1, 5, 3};
48.
49.     for(i = 0; i < 5; i++)
50.         printf("%d ", A[i]);
51.
52.     printf("\n");
53.     merge_sort(A, 0, 4);
54.
55.     for(i = 0; i < 5; i++)
56.         printf("%d ", A[i]);
57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top