Skip to content

Commit 73a13aa

Browse files
committed
added searching and sorting programs
1 parent 526c138 commit 73a13aa

11 files changed

+709
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ This repo includes:
5656
- [Pair Sum](./milestone2/arrays/PairSum.cpp)
5757
- [Triplet Sum](./milestone2/arrays/TripletSum.cpp)
5858
- [Sort 0 1](./milestone2/arrays/Sort01.cpp)
59+
- [Searching and Sorting](./milestone2/SearchingandSorting/)
60+
- [Code Binary Search](./milestone2/SearchingandSorting/CodeBinarySearch.cpp)
61+
- [Code Bubble Sort](./milestone2/SearchingandSorting/CodeBubbleSort.cpp)
62+
- [Code Insertion Sort](./milestone2/SearchingandSorting/CodeInsertionSort.cpp)
63+
- [Code Merge Two Sorted Arrays](./milestone2/SearchingandSorting/CodeMergeTwoSortedArrays.cpp)
64+
- [Push Zeros to end](./milestone2/SearchingandSorting/PushZerostoend.cpp)
65+
- [Rotate array](./milestone2/SearchingandSorting/Rotatearray.cpp)
66+
- [Second Largest in array](./milestone2/SearchingandSorting/SecondLargestinarray.cpp)
67+
- [Check Array Rotation](./milestone2/SearchingandSorting/CheckArrayRotation.cpp)
68+
- [Sort 0 1 2](./milestone2/SearchingandSorting/Sort012.cpp)
69+
- [Sum of Two Arrays](./milestone2/SearchingandSorting/SumofTwoArrays.cpp)
5970

6071
# Coding Ninjas
6172
## Rate my Repo ⭐!!!
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
You have been given an integer array/list(ARR) of size N. It has been sorted(in increasing order) and then rotated by some number 'K' (K is greater than 0) in the right hand direction.
2+
Your task is to write a function that returns the value of 'K', that means, the index from which the array/list has been rotated.
3+
Sample Input 1:
4+
1
5+
6
6+
5 6 1 2 3 4
7+
Sample Output 1:
8+
2
9+
10+
// My Code:
11+
int arrayRotateCheck(int arr[], int n) {
12+
13+
int count=1;
14+
bool check = false;
15+
for(int i = 0; i < n-1; i++){
16+
if(arr[i] <= arr[i+1]){
17+
count++;
18+
}
19+
else{
20+
check = true;
21+
break;
22+
}
23+
}
24+
if(check == false){
25+
return 0;
26+
}
27+
else{
28+
return count;
29+
}
30+
}
31+
32+
33+
// Main Code:
34+
#include <iostream>
35+
using namespace std;
36+
37+
#include "solution.h"
38+
39+
int main()
40+
{
41+
42+
int t;
43+
cin >> t;
44+
while (t--)
45+
{
46+
47+
int size;
48+
cin >> size;
49+
int *input = new int[size];
50+
51+
for (int i = 0; i < size; i++)
52+
{
53+
cin >> input[i];
54+
}
55+
56+
cout << arrayRotateCheck(input, size) << endl;
57+
delete[] input;
58+
}
59+
60+
return 0;
61+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// You are given an integer array 'A' of size 'N', sorted in ascending order. You are also given an integer 'target'.
2+
// Your task is to write a function to search for 'target' in the array 'A'. If it exists, return its index in 0-based indexing. Otherwise, return -1.
3+
4+
// Note: You must write an algorithm whose time complexity is O(logN).
5+
6+
// Example:
7+
// Input: ‘N’ = 7 ‘target’ = 3
8+
// ‘A’ = [1, 3, 7, 9, 11, 12, 45]
9+
10+
// Output: 1
11+
12+
// Explanation: For A = [1, 3, 7, 9, 11, 12, 45],
13+
// The index of element '3' is 1.
14+
// Hence, the answer is '1'.
15+
16+
// My Code:
17+
int search(vector<int>& nums, int target) {
18+
int start = 0;
19+
int end = nums.size()-1;
20+
21+
while (start <= end){
22+
int mid = (start+end)/2;
23+
if (nums[mid] == target)
24+
return mid;
25+
else if(target < nums[mid])
26+
end = mid-1;
27+
else
28+
start = mid+1;
29+
}
30+
return -1;
31+
}
32+
33+
// Main Code:
34+
#include <bits/stdc++.h>
35+
36+
using namespace std;
37+
using ll = long long;
38+
#include "solution.h"
39+
40+
41+
class Runner
42+
{
43+
int t = 1;
44+
45+
vector<int> v;
46+
int x;
47+
48+
public:
49+
void takeInput()
50+
{
51+
int n; cin >> n;
52+
assert(1 <= n && n <= (int)1e4);
53+
v.assign(n, 0);
54+
for(int i = 0; i < n; ++i) cin >> v[i];
55+
cin >> x;
56+
}
57+
void execute()
58+
{
59+
vector<int> copy = v;
60+
search(copy, x);
61+
}
62+
63+
void executeAndPrintOutput()
64+
{
65+
vector<int> copy = v;
66+
int ans = search(copy, x);
67+
cout << ans << '\n';
68+
}
69+
};
70+
71+
int main()
72+
{
73+
// freopen("./Testcases/large/in/input11.txt", "r", stdin);
74+
// freopen("./Testcases/large/out/output11.txt", "w", stdout);
75+
Runner runner;
76+
runner.takeInput();
77+
runner.executeAndPrintOutput();
78+
79+
return 0;
80+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Provided with a random integer array/list(ARR) of size N, you must sort this array using 'Bubble Sort'.
2+
// Note:
3+
// Change in the input array/list itself. You don't need to return or print the elements.
4+
5+
// Example:
6+
// Input: ‘N’ = 7
7+
// 'ARR' = [2, 13, 4, 1, 3, 6, 28]
8+
// Output: [1 2 3 4 6 13 28]
9+
// Explanation: After applying 'bubble sort' on the input array, the output is [1 2 3 4 6 13 28].
10+
11+
// My Code:
12+
void bubbleSort(int arr[], int n){
13+
int k=0;
14+
while( k < n-1){ // for n-1 round to sort the array
15+
for(int i=0;i<n-1;i++){ // for 1 round
16+
if(arr[i]>arr[i+1]){
17+
int temp = arr[i];
18+
arr[i] = arr[i+1];
19+
arr[i+1] = temp;
20+
}
21+
}
22+
k++;
23+
}
24+
}
25+
26+
// Main Code:
27+
#include<iostream>
28+
using namespace std;
29+
30+
#include"solution.h"
31+
32+
int main()
33+
{
34+
int n;
35+
cin>>n;
36+
int arr[n];
37+
for(int i=0;i<n;i++)
38+
{
39+
cin>>arr[i];
40+
}
41+
int target;
42+
cin>>target;
43+
bubbleSort(arr, n);
44+
for(int i=0;i<n;i++)
45+
{
46+
cout<<arr[i]<<" ";
47+
}
48+
return 0;
49+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Insertion Sort'.
2+
// Note:
3+
// Change in the input array/list itself. You don't need to return or print the elements.
4+
5+
// Sample Input 1:
6+
// 1
7+
// 7
8+
// 2 13 4 1 3 6 28
9+
// Sample Output 1:
10+
// 1 2 3 4 6 13 28
11+
12+
// My Code:
13+
void insertionSort(int arr[], int n){
14+
// Write your code here
15+
for(int i=1;i<n;i++){
16+
int current=arr[i];
17+
int j;
18+
for( j = i-1; j >= 0;j--){
19+
if(current < arr[j]){
20+
arr[j+1]=arr[j]; // shifting by 1 position
21+
}
22+
else{
23+
break;
24+
}
25+
}
26+
arr[j+1]=current;
27+
}
28+
}
29+
30+
// Main Code:
31+
#include <iostream>
32+
using namespace std;
33+
34+
#include "solution.h"
35+
36+
int main()
37+
{
38+
int t;
39+
cin >> t;
40+
41+
while (t--)
42+
{
43+
int size;
44+
cin >> size;
45+
int input[size];
46+
47+
for (int i = 0; i < size; i++)
48+
{
49+
cin >> input[i];
50+
}
51+
52+
insertionSort(input, size);
53+
54+
for (int i = 0; i < size; i++)
55+
{
56+
cout << input[i] << " ";
57+
}
58+
59+
cout << endl;
60+
}
61+
62+
return 0;
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// You have been given two sorted arrays/lists(ARR1 and ARR2) of size N and M respectively, merge them into a third array/list such that the third array is also sorted.
2+
3+
// Note: Consider the case when size of the two arrays is not same.
4+
// Sample Input 1 :
5+
// 1
6+
// 5
7+
// 1 3 4 7 11
8+
// 4
9+
// 2 4 6 13
10+
// Sample Output 1 :
11+
// 1 2 3 4 4 6 7 11 13
12+
13+
// My Code:
14+
void merge(int arr1[], int size1, int arr2[], int size2, int ans[]){
15+
int i = 0, j = 0, k = 0;
16+
while(i < size1 && j < size2){
17+
if(arr1[i] < arr2[j]){
18+
ans[k++] = arr1[i++];
19+
}
20+
else{
21+
ans[k++] = arr2[j++];
22+
}
23+
}
24+
while(i < size1){
25+
ans[k++] = arr1[i++];
26+
}
27+
while(j < size2){
28+
ans[k++] = arr2[j++];
29+
}
30+
}
31+
32+
// Main Code:
33+
#include <iostream>
34+
using namespace std;
35+
36+
#include "solution.h"
37+
38+
int main()
39+
{
40+
int t;
41+
cin >> t;
42+
43+
while (t--)
44+
{
45+
int size1;
46+
cin >> size1;
47+
48+
int arr1[size1];
49+
50+
for (int i = 0; i < size1; i++)
51+
{
52+
cin >> arr1[i];
53+
}
54+
55+
int size2;
56+
cin >> size2;
57+
58+
int arr2[size2];
59+
60+
for (int i = 0; i < size2; i++)
61+
{
62+
cin >> arr2[i];
63+
}
64+
65+
int ans[size1+size2];
66+
67+
merge(arr1, size1, arr2, size2, ans);
68+
69+
for (int i = 0; i < size1 + size2; i++)
70+
{
71+
cout << ans[i] << " ";
72+
}
73+
74+
cout << endl;
75+
76+
}
77+
}

0 commit comments

Comments
 (0)