Skip to content

Commit

Permalink
Merge pull request #14 from Salonisaroha/new-branch
Browse files Browse the repository at this point in the history
Stuff related to Array concepts.
  • Loading branch information
pravocodes authored Oct 4, 2024
2 parents f30b3ca + 5b3ead4 commit d32594e
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 0 deletions.
22 changes: 22 additions & 0 deletions C++/QuestionsOfArrays/2Darray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
int main(){
int n,m;
cout<<"Enter number of rows and columns\n";
cin>>n;
cin>>m;
int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0; j<m; j++){
cin>>arr[i][j];
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}


}
22 changes: 22 additions & 0 deletions C++/QuestionsOfArrays/bubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
using namespace std;
void printArray(int arr[], int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void Bubble(int *arr, int n){
for(int i = 0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
swap(arr[j], arr[j+1]);
}
}
}
printArray(arr,n);
}
int main(){
int arr[5] = {7,6,5,4,3};
int n = sizeof(arr)/sizeof(int);
Bubble(arr, n);
}
24 changes: 24 additions & 0 deletions C++/QuestionsOfArrays/charSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
void printArray(char arr[], int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void charSort(char arr[], int n){
for(int i = 1; i<n; i++){
int curr = arr[i];
int prev = i-1;
while(prev>=0 && arr[prev]<curr){
swap(arr[prev], arr[prev+1]);
prev--;
}
arr[prev+1] = curr;
}
printArray(arr,n);
}
int main(){
char arr[] = {'a', 'q','e','z','o','y','b','n','k','g','f'};
int n= sizeof(arr)/sizeof(arr[0]);
charSort(arr,n);
}
46 changes: 46 additions & 0 deletions C++/QuestionsOfArrays/countSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
using namespace std;

void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

void countSort(int *arr, int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

int countArray[max + 1] = {0};

for (int i = 0; i < n; i++) {
countArray[arr[i]]++;
}

for (int i = 1; i <= max; i++) {
countArray[i] += countArray[i - 1];
}

int outputArray[n];
for (int i = n - 1; i >= 0; i--) {
outputArray[countArray[arr[i]] - 1] = arr[i];
countArray[arr[i]]--;
}

for (int i = 0; i < n; i++) {
arr[i] = outputArray[i];
}
}

int main() {
int arr[10] = {10,3,5,2,8,5,9,3,0,1};
int n = 10;
countSort(arr, n);
printArray(arr, n);
return 0;
}
29 changes: 29 additions & 0 deletions C++/QuestionsOfArrays/diagonalSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<iostream>
using namespace std;
int diagonalSum(int mat[][4], int n){
int sum = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i==j){
sum += mat[i][j];
}
else if(j=n-i-1){
sum += mat[i][j];
}
}
}
cout<<"Sum of diagonal is "<<sum<<endl;
return sum;
}
int main(){
int mat[4][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};

diagonalSum(mat,4);
cout<<"hello world!";
return 0;
}
32 changes: 32 additions & 0 deletions C++/QuestionsOfArrays/insertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<iostream>
using namespace std;
void printArray(int arr[], int n){
for(int i = 0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void insertionSort(int arr[], int n){
for(int i = 1; i<n; i++){
int curr = arr[i];
int prev = i-1;
while(prev>=0 && arr[prev]>curr){
swap(arr[prev], arr[prev+1]);
prev--;
}
arr[prev+1] = curr;
}
printArray(arr,n);

}
int main(){
int n;
cout<<"Enter number of elements"<<endl;
cin>>n;
int arr[n];
cout<<"enter array elements"<<endl;
for(int i = 0; i<n; i++){
cin>>arr[i];
}
insertionSort(arr,n);

}
24 changes: 24 additions & 0 deletions C++/QuestionsOfArrays/selectionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
void print(int *arr, int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void Selection(int *arr, int n){
for(int i=0; i<n-1; i++){
int minIdx = i;
for(int j = i+1; j<n; j++){
if(arr[j]<arr[minIdx]){
minIdx = j;
}
}
swap(arr[i], arr[minIdx]);
}
print(arr,n);
}
int main(){
int arr[7] = {9,8,5,6,4,8,3};
int n = sizeof(arr)/sizeof(int);
Selection(arr, n);
}
45 changes: 45 additions & 0 deletions C++/QuestionsOfArrays/spiralMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<iostream>
using namespace std;
void spiralMatrix(int mat[][4], int n, int m){
int srow = 0, scol = 0;
int erow = n-1, ecol = m-1;
while(srow<=erow && scol<=ecol){
//top
for(int j=scol; j<=ecol; j++){
cout<<mat[srow][j]<<" ";
}
//right
for(int j=srow+1; j<=erow; j++){
cout<<mat[j][ecol]<<" ";
}
//bottom
for(int j=ecol-1; j>=scol; j--){
if(srow==erow){
break;
}
cout<<mat[erow][j]<<" ";
}
//left
for(int j=erow-1; j>= srow+1; j--){
if(scol==ecol){
break;
}
cout<<mat[j][scol]<<" ";
}
srow++;
scol++;
erow--;
ecol--;
}

}
int main(){
int matrix[4][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
spiralMatrix(matrix, 4, 4);
return 0;
}
35 changes: 35 additions & 0 deletions C++/QuestionsOfArrays/trappingRainwater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<iostream>
#include<climits>
using namespace std;
void trap(int *heights, int n){
int leftMax[20000], rightMax[20000];
leftMax[0] = heights[0];
rightMax[n-1]= heights[n-1];
cout<<leftMax[0]<<" ,";
for(int i=1; i<n; i++){
leftMax[i] = max(leftMax[i-1], heights[i-1]);
cout<<leftMax[i]<<" , ";
}
cout<<endl;

for(int i = n-2; i>=0; i--){
rightMax[i]= max(rightMax[i+1], heights[i+1]);
cout<<rightMax[i]<<",";
}
cout<<rightMax[n-1]<<",";
int waterTrapped = 0;
for(int i = 0; i<n; i++){
int currWater = min(leftMax[i], rightMax[i]) - heights[i];
if(currWater>0){
waterTrapped += currWater;
}
}
cout<<waterTrapped;
cout<<endl;
}
int main(){
int heights[7] = {4,2,0,6,3,2,5};
int n = sizeof(heights)/sizeof(int);
trap(heights, n);
return 0;
}
92 changes: 92 additions & 0 deletions cpp/dijkestra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <bits/stdc++.h>
using namespace std;

// Number of vertices in the graph
#define V 9

// A utility function to print the constructed distance
// array
void printSolution(vector<int>& dist)
{
cout << "Vertex Distance from Source\n";
for (int i = 0; i < V; i++)
cout << "\t" << i << " \t\t\t\t " << dist[i] << endl;
}

// Function that implements Dijkstra's single source shortest path algorithm
void dijkstra(vector<vector<pair<int, int>>> &graph, int src)
{
// Priority queue to store (distance, vertex) pairs
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

// Vector to store the shortest distance from source to each vertex
vector<int> dist(V, INT_MAX);

// Insert the source node and initialize its distance as 0
pq.push({0, src});
dist[src] = 0;

// While there are vertices left to process
while (!pq.empty()) {
// Extract the vertex with minimum distance value
int u = pq.top().second;
pq.pop();

// Traverse through all adjacent vertices of u
for (auto& adj : graph[u]) {
int v = adj.first;
int weight = adj.second;

// If there is a shorter path to v through u
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}

// Print the constructed distance array
printSolution(dist);
}

// Driver program to test the above function
int main()
{
// Create a graph with adjacency list representation
vector<vector<pair<int, int>>> graph(V);

// Add edges with (vertex, weight) pairs
graph[0].push_back({1, 4});
graph[0].push_back({7, 8});
graph[1].push_back({0, 4});
graph[1].push_back({2, 8});
graph[1].push_back({7, 11});
graph[2].push_back({1, 8});
graph[2].push_back({3, 7});
graph[2].push_back({5, 4});
graph[2].push_back({8, 2});
graph[3].push_back({2, 7});
graph[3].push_back({4, 9});
graph[3].push_back({5, 14});
graph[4].push_back({3, 9});
graph[4].push_back({5, 10});
graph[5].push_back({2, 4});
graph[5].push_back({3, 14});
graph[5].push_back({4, 10});
graph[5].push_back({6, 2});
graph[6].push_back({5, 2});
graph[6].push_back({7, 1});
graph[6].push_back({8, 6});
graph[7].push_back({0, 8});
graph[7].push_back({1, 11});
graph[7].push_back({6, 1});
graph[7].push_back({8, 7});
graph[8].push_back({2, 2});
graph[8].push_back({6, 6});
graph[8].push_back({7, 7});

// Call Dijkstra's algorithm
dijkstra(graph, 0);

return 0;
}

0 comments on commit d32594e

Please sign in to comment.