Professional Documents
Culture Documents
AAA - Task 1
AAA - Task 1
Step1 ALGORITHM
Step2 CODE
Sorting Algorithms
Bubble Sort
Step1 ALGORITHM
begin BubbleSort(list)
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Time Complexity:
Step2 CODE
Implementation in C
#include <stdio.h>
#include <stdbool.h>
#define MAX 10
int list[MAX] = {1,8,4,6,0,3,5,2,7,9};
void display(){
int i;
printf("[");
printf("%d ",list[i]);
printf("]\n");
void bubbleSort() {
int temp;
int i,j;
swapped = false;
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = true;
printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
}else {
loop. if(!swapped) {
break;
display();
main(){
display(); printf("\n");
bubbleSort();
printf("\nOutput Array:
"); display();
Insertion Sort
Step1 ALGORITHM
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step2 CODE
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
void printline(int
count){ int i;
printf("=");
printf("=\n");
void display(){
int i;
printf("[");
printf("%d ",intArray[i]);
}
printf("]\n");
void insertionSort(){
int valueToInsert;
int holePosition;
int i;
valueToInsert = intArray[i];
holePosition = i;
intArray[holePosition] = intArray[holePosition-1];
holePosition--;
if(holePosition != i){
valueToInsert,holePosition);
intArray[holePosition] = valueToInsert;
printf("Iteration %d#:",i);
display();
main(){
printf("Input Array: ");
display();
printline(50);
insertionSort();
printf("Output Array:
"); display();
printline(50);
Selection Sort
Step1 ALGORITHM
Step2 CODE
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
void printline(int
count){ int i;
printf("=");
}
printf("=\n");
void display(){
int i;
printf("[");
printf("]\n");
void selectionSort(){
int indexMin,i,j;
minimum indexMin = i;
for(j = i+1;j<MAX;j++){
indexMin = j;
if(indexMin != i){
intArray[indexMin]);
intArray[indexMin] =
intArray[i]; intArray[i] = temp;
printf("Iteration %d#:",(i+1));
display(); }}
main(){
display();
printline(50);
selectionSort();
display();
printline(50);
Merge Sort
Step1 ALGORITHM
Step 2 − divide the list recursively into two halves until it can no more be
divided.
Step 3 − merge the smaller lists into new list in sorted order.
Time Complexity:
Step2 CODE
#include <stdio.h>
#define max 10
int a[10] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
b[i++] = a[l1++];
b[i++] = a[l2++];
a[i] = b[i];
{ int mid;
sort(low, mid);
sort(mid+1, high);
}else {
return;
}
int main()
{ int i;
sort(0, max);
Quick Sort
Step1 ALGORITHM
PIVOT
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
Step2 CODE
#include <stdio.h>
#include<stdbool.h>
#define MAX 7
void printline(int
count){ int i;
printf("=\n");
void display(){
int i;
printf("[");
printf("%d ",intArray[i]);
printf("]\n");
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int
while(true){
while(intArray[++leftPointer] <
if(leftPointer >=
rightPointer){ break;
}else{
intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
intArray[leftPointer],intArray[right]); swap(leftPointer,right);
display();
return leftPointer;
return;
}else {
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
main(){
display();
printline(50);
quickSort(0,MAX-1);
printf("Output Array:
"); display();
printline(50);
Heap Sort
Step1 ALGORITHM
Step 3 - Delete the root element from Min Heap using Heapify method.
Time Complexity:
#include<stdio.h>
int val;
int largest = i;
if (left < size && arr[left] >arr[largest]) largest = left; if (right < size && arr[right] > arr[largest])
largest = right; if (largest != i)
val = arr[i];
arr[i]= arr[largest];
arr[largest] = val;
int i;
val = arr[0];
arr[0]= arr[i];
arr[i] = val;
heapify(arr, i, 0);
}
void main()
int arr[] = {20, 50, 40, 10, 90, 80, 60, 70, 30, 100};
int i;
heapSort(arr, size);
printf("%d\n",arr[i]); }
Searching Algorithms
Linear Search
Step1 ALGORITHM
Step 2: Compare the current element with the target element. If matches, then go to step 5.
Step 3: If there is a next element, then set current element to next element and go to Step 2.
Complexity
Binary Search
Step1 ALGORITHM
binarySearch(arr, size)
if (item == arr[midIndex] )
return midIndex
beg = midIndex + 1
else
end = midIndex – 1
Step2 CODE
let start = 0;
return middle;
} else if (sortedArray[middle] < key) {
start = middle + 1;
} else {
end = middle - 1;
return -1;
Step2 CODE
array_type variable_name[array_size];
int Age[10];
Stacks
Step1 ALGORITHM
A Stack is a data structure following the LIFO(Last In, First Out) principle
Push Operation
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Pop Operation
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step2 CODE
void pop()
if ( isEmpty() == True )
else
top = top - 1
if ( top == capacity - 1 )
arr[top+1] = item
top = top + 1
Queues
Step1 ALGORITHM
Queue is a data structure following the FIFO(First In, First Out) principle
Queue(int cap)
capacity = cap
front = -1
back = -1
Step 2 Code
print("Queue if full!")
else
arr[back] = item
if(front == -1)
front = back
}
int dequeue()
if(isEmpty() == True)
print("Queue is empty!")
return 0
else
if(front == back)
front = back = -1
else
return item
Linked List
Step1 ALGORITHM
class ListNode
int val
ListNode next
Step 2 Code
void enqueue(int item)
front = temp
back = temp
else
back.next = temp
back = back.next
Dequeue Operation
int dequeue()
if ( isEmpty() == True )
return 0
else
front = front.next
free(temp)
return item
while(temp != NULL) {
printf("%d --->",temp->data);
temp = temp->next;
Insert
newNode->data = 4;
newNode->next = head;
head = newNode;
head = head->next;
while(temp->next->next!=NULL){
temp = temp->next;
}
temp->next = NULL;
Search a node
current = current->next;
return false;
int temp;
if (head_ref == NULL) {
return;
} else {
index = current->next;
current->data = index->data;
index->data = temp;
index = index->next;
current = current->next;
Complexity Analysis:
Time Complexity: O(2n).
As there are redundant subproblems.
Auxiliary Space :O(1) + O(N).
Step2 CODE
#include <bits/stdc++.h>
// Base Case
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
else
return max(
val[n - 1]
// Driver code
int main()
{
int val[] = { 60, 100, 120 };
int W = 50;
return 0;