12thdecACA
12thdecACA
12thdecACA
7 Sorting
7.1 Insertion Sort
Algorithm:
1. Iterate through the array from the second element to the last element.
2. For each element, store its value in a temporary variable.
3. Compare the temporary value with elements in the sorted portion of the array (elements to its left):
Time Complexity:
• Best Case: O(n): When the array is already sorted, only n − 1 comparisons are needed.
• Worst Case: O(n2 ): When the array is sorted in reverse order, the inner loop runs for every element.
• Average Case: O(n2 ): On average, elements are halfway out of place.
Space Complexity:
• O(1): Sorting is done in-place, requiring no extra memory.
Code:
# include < iostream >
# include < vector >
using namespace std ;
int main ()
{
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
insertionSort ( arr ) ;
42
cout << endl ;
return 0;
}
Output 1:
Enter the number of elements: 5
Enter the elements: 12 11 13 5 6
Sorted array: 5 6 11 12 13
Output 2:
Enter the number of elements: 4
Enter the elements: 20 10 40 30
Sorted array: 10 20 30 40
int hoarePa rtition ( vector < int >& arr , int low , int high ) {
int pivot = arr [ low ];
int i = low - 1;
int j = high + 1;
while ( true ) {
do {
i ++;
} while ( arr [ i ] < pivot ) ;
do {
j - -;
43
} while ( arr [ j ] > pivot ) ;
if ( i >= j ) {
return j ;
}
swap ( arr [ i ] , arr [ j ]) ;
}
}
void quickSort ( vector < int >& arr , int low , int high ) {
if ( low < high ) {
int p = hoar ePartit ion ( arr , low , high ) ;
quickSort ( arr , low , p ) ;
quickSort ( arr , p + 1 , high ) ;
}
}
int main () {
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
quickSort ( arr , 0 , n - 1) ;
return 0;
}
Output 1:
44
Code:
# include < iostream >
# include < vector >
using namespace std ;
void merge ( vector < int >& arr , int left , int mid , int right ) {
int n1 = mid - left + 1;
int n2 = right - mid ;
int i = 0 , j = 0 , k = left ;
while ( i < n1 && j < n2 )
{
if ( leftArr [ i ] <= rightArr [ j ])
arr [ k ++] = leftArr [ i ++];
else
arr [ k ++] = rightArr [ j ++];
}
while ( i < n1 )
arr [ k ++] = leftArr [ i ++];
while ( j < n2 )
arr [ k ++] = rightArr [ j ++];
}
void mergeSort ( vector < int >& arr , int left , int right ) {
if ( left < right ) {
int mid = left + ( right - left ) / 2;
int main () {
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
mergeSort ( arr , 0 , n - 1) ;
return 0;
}
Output 1:
Enter the number of elements: 5
Enter the elements: 12 11 13 5 6
Sorted array: 5 6 11 12 13
Output 2:
Enter the number of elements: 6
Enter the elements: 38 27 43 3 9 82
45
Sorted array: 3 9 27 38 43 82
8 Recursion
8.1 Fibonacci Sequence using recursion
Algorithm:
1. Define a recursive function fibonacci(n):
• Base cases:
– If n = 0, return 0.
– If n = 1, return 1.
• Recursive case: Return the sum of fibonacci(n - 1) and fibonacci(n - 2).
2. In the main function:
• Take the number of terms (n) as input.
• Print the Fibonacci sequence by calling the fibonacci function iteratively for indices 0 to n − 1.
Time Complexity:
• O(2n ): Each call splits into two further calls, resulting in exponential growth of computations.
Space Complexity:
• O(n): Due to recursive function call stack.
Code:
# include < iostream >
using namespace std ;
int main ()
{
int n ;
cout << " Enter the number of terms : " ;
cin >> n ;
if ( n <= 0)
{
cout << " Number of terms must be positive . " << endl ;
return 0;
}
return 0;
}
Output 1:
Enter the number of terms: 5
Fibonacci Sequence: 0 1 1 2 3
Output 2:
Enter the number of terms: 8
Fibonacci Sequence: 0 1 1 2 3 5 8 13
46
8.2 Fibonacci Sequence using Memo Table
Algorithm:
1. Create a memo table (array) to store Fibonacci numbers, initialized with -1.
2. Define a recursive function fibonacci(n):
• Base cases:
– If n = 0, return 0.
– If n = 1, return 1.
• If the Fibonacci number for n is already computed (i.e., memo[n] != -1), return memo[n].
• Otherwise, compute fibonacci(n - 1) + fibonacci(n - 2), store it in memo[n], and return the result.
3. In the main function:
• Take the number of terms (n) as input.
• Print the Fibonacci sequence by calling the fibonacci function iteratively for indices 0 to n − 1.
Time Complexity:
• O(n): Each Fibonacci number is computed only once and stored in the memo table.
Space Complexity:
if ( memo [ n ] != -1)
return memo [ n ];
int main ()
{
int n ;
cout << " Enter the number of terms : " ;
cin >> n ;
if ( n <= 0)
{
cout << " Number of terms must be positive . " << endl ;
return 0;
}
return 0;
}
Output 1:
47
Enter the number of terms: 5
Fibonacci Sequence: 0 1 1 2 3
Output 2:
Enter the number of terms: 8
Fibonacci Sequence: 0 1 1 2 3 5 8 13
Space Complexity:
• O(n): Due to the recursive call stack.
Code:
# include < iostream >
using namespace std ;
int main ()
{
int n ;
cout << " Enter the number of disks : " ;
cin >> n ;
if ( n <= 0)
{
cout << " Number of disks must be positive . " << endl ;
return 0;
}
cout << " The sequence of moves to solve Tower of Hanoi is :\ n " ;
towerOfHanoi (n , ’A ’ , ’C ’ , ’B ’) ;
return 0;
}
Output 1:
48
Enter the number of disks: 2
The sequence of moves to solve Tower of Hanoi is:
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Output 2:
Enter the number of disks: 3
The sequence of moves to solve Tower of Hanoi is:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
49