[go: up one dir, main page]

0% found this document useful (0 votes)
29 views4 pages

Arraysıralama

This C++ program contains 3 functions to sort matrices in ascending order. The first function takes in a 2D array and integer values for its size, reads in the matrix elements, and performs an in-place bubble sort. The second function similarly bubble sorts a 2D array but with nested loops. The third function defines a void swap function and uses it to bubble sort a 2D array by row. It reads in a square matrix, bubble sorts each row in ascending order using the swap function, and outputs the sorted matrix.

Uploaded by

pnrylmz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Arraysıralama

This C++ program contains 3 functions to sort matrices in ascending order. The first function takes in a 2D array and integer values for its size, reads in the matrix elements, and performs an in-place bubble sort. The second function similarly bubble sorts a 2D array but with nested loops. The third function defines a void swap function and uses it to bubble sort a 2D array by row. It reads in a square matrix, bubble sorts each row in ascending order using the swap function, and outputs the sorted matrix.

Uploaded by

pnrylmz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

using namespace std;

int main()
{
int array1[10][10], array2[10][10];
int i, j, k, a, m, n;

cout<<"Enter the order of the matrix \n";


cin>>n;
cout<<"Enter co-efficients of the matrix \n";
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
cin>>array1[i][j];
}
}

for (i = 0; i < n; ++i)


{
for (j = 0; j < n; ++j)
{
for (k =j+1; k < n; ++k)
{
if (array1[i][j] > array1[i][k])
{
a = array1[i][j];
array1[i][j] = array1[i][k];
array1[i][k] = a;
}
}
}
}
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
cout<< array1[i][j]<<" ";
}
cout<<endl;
} return 0;
}
#include <iostream>
using namespace std;

int main() {
int a[20][20];
int temp[20][20];
int n, i, j, l, k;

cout << "Enter the matrix size: ";


cin >> n;
cout << "Enter the matrix values: " << endl;
for (j = 0; j < n; j++) {
for (i = 0; i < n; i++) {
cin >> a[j][i];
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (l = i; l < n; l++) {
if (l == i) {
for (k = (j + 1); k < n; k++) {
if (a[i][j] > a[l][k]) {
temp[i][j] = a[i][j];
a[i][j] = a[l][k];
a[l][k] = temp[i][j];
}
} } } }}
if (l > i) {
for (k = 0; k < n; k++) {
if (a[i][j] > a[l][k]) {
temp[i][j] = a[i][j];
a[i][j] = a[l][k];
a[l][k] = temp[i][j];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; i++) {
cout << a[i][j];
}
}
cout << endl;

return 0;

#include <iostream>

using namespace std;

void order(int &x, int &y){

int temp=x;

x=y;

y=temp;

}
int main() {

int n,x;

int My [20][20];

cout<<"Enter a value for square matrix:";

cin>>n;

cout<<"Enter elements of matrix:";

for(int i=0; i<n; i++) {

for(int j=0; j<n; j++) {

cin>>My[i][j];

for( int k=0; k<n; k++){

for(int m=0; m<n; m++){

x=m+1;

for(int i=k; i<n; i++){

for(int j=x;j<n; j++){

if(My[k][m]>My[i][j]) {

order(My[k][m],My[i][j]);

x=0;

for(int i=0; i<n;i++) {

for(int j=0; j<n; j++){

cout<<My[i][j]<<" ";
}

cout<<endl;

return 0;

You might also like