-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_array_DSA_Problem_1_Array.cpp
72 lines (60 loc) · 1.47 KB
/
reverse_array_DSA_Problem_1_Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<vector>
using namespace std;
//displaying all elements of an array
int display_array(vector<int> arr)
{
for (int i = 0; i < arr.size(); i++)
{
cout<<arr[i]<<"||";
}
}
//swaping elements on array positions
vector<int> swap(int current_position , int destination_postion , vector<int> arr)
{
int t = arr[destination_postion];
arr[destination_postion] = arr[current_position];
arr[current_position] = t ;
return arr;
}
//bruteforce method for revering an array.
//simply swaping elements from fst to last index and so on.
vector<int> reversing_array_iterative_method(vector<int> arr)
{
int temp;
for (int i = 0 , j = arr.size() - 1; i < arr.size()/2 ; i++ , j--)
{
arr = swap(i ,j , arr);
}
return arr ;
}
//recursive approch
void reversing_array_using_recursion(int arr[] , int start , int end)
{
if( start >= end )
return ;
int temp = arr[end];
arr[end] = arr[start];
arr[start] = temp;
reversing_array_using_recursion(arr,start + 1, end - 1);
}
void disp(int arr[])
{
for(int i = 0 ; i < 7 ; i++)
{
cout<<arr[i]<<"||";
}
}
int main()
{
vector<int> arr{0,1,2,3,4,5,6,7,8};
int arr2[7] = {0,1,2,3,4,5,6};
int start = 0 ;
int end = 6;
disp(arr2);
// arr = reversing_array_iterative_method(arr);
reversing_array_using_recursion(arr2 , start , end );
cout<<"Reversed"<<endl;
disp(arr2);
return 0 ;
}