Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- void merge(int a[], int left, int mid, int right)
- {
- int n1 = mid - left + 1;
- int n2 = right - mid;
- int left_array[n1];
- int right_array[n2];
- for (int i = 0; i < n1; ++i) left_array[i] = a[left + i];
- for (int i = 0; i < n2; ++i) right_array[i] = a[mid + 1 + i];
- int i = 0;
- int j = 0;
- int index = left;
- while ((i < n1) && (j < n2))
- {
- if (left_array[i] < right_array[j])
- {
- a[index] = left_array[i];
- ++i;
- }
- else
- {
- a[index] = right_array[j];
- ++j;
- }
- ++index;
- }
- while (i < n1)
- {
- a[index] = left_array[i];
- ++i;
- ++index;
- }
- while (j < n2)
- {
- a[index] = right_array[j];
- ++j;
- ++index;
- }
- }
- void merge_sort(int a[], int left, int right)
- {
- if (left >= right) return;
- int mid = (left + right) / 2;
- merge_sort(a, left, mid);
- merge_sort(a, mid + 1, right);
- merge(a, left, mid, right);
- }
- */
- void insertion_sort(int data[], int n)
- {
- for (int i = 1; i < n; i++)
- {
- // Tìm vị trí phù hợp cho i
- int j = i;
- while (j > 0 && data[i] < data[j-1]) --j;
- // Đưa i về đúng vị trí
- int tmp = data[i];
- for (int k = i; k > j; k--)
- data[k] = data[k-1];
- data[j] = tmp;
- }
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- int a[n];
- for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
- insertion_sort(a, n);
- printf("%d %d %d", a[n - 1], a[n - 2], a[n - 3]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement