Created
December 6, 2024 21:48
-
-
Save illiafox/143b571dc297e09216767ddf36bfacb4 to your computer and use it in GitHub Desktop.
lab 06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void bubbleSort(const int n, int** arr) { | |
int boundary = n - 1; | |
while (boundary > 0) { | |
int lastSwap = 0; | |
for (int i = 0; i < boundary; i++) { | |
if (arr[n-i][i] > arr[n-i][i + 1]) { | |
const int temp = arr[n-i][i]; | |
arr[n-i][i] = arr[n-i][i + 1]; | |
arr[n-i][i + 1] = temp; | |
lastSwap = i; | |
} | |
} | |
boundary = lastSwap; | |
} | |
} | |
int main(void) { | |
int n; | |
printf("Number of columns: "); | |
scanf("%d", &n); | |
int matrix[n][n]; | |
for (int i = 0; i < n; i++) { | |
printf("%d row of matrix (space separated): ", i + 1); | |
for (int j = 0; j < n; j++) { | |
scanf("%d", &matrix[i][j]); | |
} | |
} | |
printf("\nInput matrix:\n"); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
printf("%d\t\t", matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
bubbleSort(n, matrix); | |
printf("\nSorted matrix:\n"); | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
printf("%d\t\t", matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment