0% found this document useful (0 votes)
66 views

Shell Sort

The document describes the shell sort algorithm in C++. It defines a shellsort class with methods to get user input for array elements, perform the sort logic using the shell sort approach, and display the sorted array. The sort logic method implements the shell sort by starting with larger increments and reducing the increments by half in each iteration to sort the array.

Uploaded by

anam wazir
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)
66 views

Shell Sort

The document describes the shell sort algorithm in C++. It defines a shellsort class with methods to get user input for array elements, perform the sort logic using the shell sort approach, and display the sorted array. The sort logic method implements the shell sort by starting with larger increments and reducing the increments by half in each iteration to sort the array.

Uploaded by

anam wazir
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/ 3

Shell sort

#include <iostream>
#include <conio.h>
#define MAX 10

using namespace std;


class shellsort{
int arr[MAX], n;
public:
void getdata();
void showdata();
void sortLogic();
};

void shellsort::getdata(){
cout << "How many elements you require : ";
cin >> n;
for (int i = 0; i<n; i++)
cin >> arr[i];
}

void shellsort::showdata(){
cout << "\n--Display--\n";
for (int i = 0; i<n; i++)
cout << arr[i] << " ";
}

void shellsort::sortLogic(){
int i, j, temp, increment;

for (increment = n / 2; increment>0; increment /= 2){


for (i = increment; i<n; i++){
temp = arr[i];
for (j = i; j >= increment; j -= increment){
if (temp < arr[j - increment])
arr[j] = arr[j - increment];
else break;
}
arr[j] = temp;
}
}
}

void main(){

cout << "\n*****Shell Sort*****\n";


shellsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
_getch();
}
MATRIX MULTIPLICATION
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;

// If column of first matrix in not equal to row of second matrix,


// ask the user to enter the size of matrix again.
while (c1 != r2)
{
cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}

// Storing elements of first matrix.


cout << endl << "Enter elements of matrix 1:" << endl;
for (i = 0; i < r1; ++i)
for (j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

// Storing elements of second matrix.


cout << endl << "Enter elements of matrix 2:" << endl;
for (i = 0; i < r2; ++i)
for (j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

// Initializing elements of matrix mult to 0.


for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
{
mult[i][j] = 0;
}

// Multiplying matrix a and b and storing in array mult.


for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
for (k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}

// Displaying the multiplication of two matrix.


cout << endl << "Output Matrix: " << endl;
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if (j == c2 - 1)
cout << endl;
}

_getch();
}

You might also like