c code for bubble sort

  • cstructuresorting
  • write a c code for bubble sort

    WAP to sort an array of n dates in an ascending order using Bubble sort. Date structure is {day, month, year }

    Code

    #include <stdio.h> typedef struct date { int day; int month; int year; } dt; void bubbleSort(dt *A, int n) { dt temp; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (A[j].year > A[j + 1].year) { temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; } } } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (A[j].month > A[j + 1].month) { if (A[j].year == A[j + 1].year) { temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; } } } } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (A[j].day > A[j + 1].day) { if (A[j].month == A[j + 1].month) { temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; } } } } } int main() { int n = 5; dt dates[n]; for (int i = 0; i < n; i++) { scanf("%d%d%d", &dates[i].day, &dates[i].month, &dates[i].year); } for (int i = 0; i < n; i++) { printf("%d/%d/%d\n", dates[i].day, dates[i].month, dates[i].year); } bubbleSort(dates, n); printf("\n"); printf("\n"); for (int i = 0; i < n; i++) { printf("%d/%d/%d\n", dates[i].day, dates[i].month, dates[i].year); } return 0; }