Merge Sort Based On Link List
Merge Sort Based On Link List
# include <iostream>
struct node{
int data;
struct node *next;
};
void insert(struct node **head, int data){
struct node *newNode = new(struct node);
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
void display(struct node *head){
struct node *temp = head;
while(temp->next != nullptr){
std::cout<< temp->data <<" ";
temp = temp->next;
}
}
void splitInTwo(struct node *head, struct node **fRef, struct node **sRef){
struct node *slow, *fast;
if(head == nullptr or head->next == nullptr){
*fRef = head;
*sRef = nullptr;
}else{
slow = fast = head;
while( fast->next != nullptr and fast->next->next != nullptr){
slow = slow->next;
fast = fast->next->next;
}
*fRef = head;
*sRef = slow->next;
slow->next = nullptr;
}
struct node *sortedMerge(struct node *fRef, struct node *sRef){
struct node *result = nullptr;
if(fRef == nullptr)
return sRef;
else if(sRef == nullptr)
return fRef;
if(fRef->data <= sRef->data){
result = fRef;
result->next = sortedMerge(fRef->next, sRef);
}else{
result = sRef;
result->next = sortedMerge(fRef,sRef->next);
}
return result;
}
void mergeSort(struct node **headRef){
struct node *a, *b;
struct node *head = *headRef;
if(head == nullptr or head->next == nullptr)
return;
splitInTwo(head,&a,&b);
mergeSort(&a);
mergeSort(&b);
}
*headRef = sortedMerge(a,b);
insert(&head, 42);
//display(head);
mergeSort(&head);
display(head);
return 0;
}
if(largest != loc){
tempVal = A[loc-1];
A[loc-1] = A[largest-1];
A[largest-1] = tempVal;
maxHeapify(A, size, largest);
}
Algorithm
Worst Case
running time
Average Case
Expected Running Time
insertion Sort
Merge Sort
HeapSort
QuickSort
Counting Sort
Radix Sort
Bucket Sort
O(n^2)
O(nlg n)
O(nlg n)
O(n^2)
O(k+n)
O(d(n+k))
O(n^2)
O(n^2)
O(nlg n)
O(nlg n) (expected)
O(k+n)
O(d(n+k))
O(n)
(average case)
}
temp->next = nullptr;
newHead = quickSortRecur(newHead, temp);
temp = getTail(newHead);
temp->next = pivot;
}
pivot->next = quickSortRecur(pivot->next, newEnd);
return newHead;
}
/* main quick-sort routine*/
void quickSort(struct node **headRef){
(*headRef) = quickSortRecur(*headRef, getTail(*headRef));
}
int main(int argc, char * argv[]){
struct node *head = nullptr;
insert(&head,10);
insert(&head, 2);
insert(&head,7);
insert(&head, 15);
insert(&head,11);
insert(&head, 9);
insert(&head,1);
insert(&head, 4);
insert(&head,31);
insert(&head, 29);
insert(&head,121);
insert(&head, 42);
quickSort(&head);
display(head);
return 0;
struct list {
struct list *next;
};
struct list *reverse(struct list *cur, struct list *prev) {
struct list *next = cur->next;
cur->next = prev;
if(next == NULL) return cur;
return reverse(next, cur);
}
reverse(head, NULL);
/* Find the ancestors of a node in Tree */
bool printAncestors(struct node *root, int target)
{
/* base cases */
if (root == NULL)
return false;
if (root->data == target)
return true;
/* If target is present in either left or right subtree of this node,
// selecting pivot
/* Ceveats !*/
i++ makes a copy, increases _i and returns the copy (old value)
++i increases _i, and returns _i.
/* Function Pointer */
#include <iostream>
using namespace std;
int addition (int a, int b){
return (a+b);
}
int subtraction (int a, int b){
return (a-b);
}
int operation (int x, int y, int (*functocall)(int,int)){
int g;
g = (*functocall)(x,y);
return (g);
}
int main (){
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
# include<iostream>
# include<stack>
# include<iostream>
# include<queue>
:
:
:
number |= 1 << x
number &= ~(1<<x)
number ^= (1<<x)
bit Variable:
struct bits{
unsigned int a:1;
unsigned int b:1;
};
struct bits mybits;
/* Atoi implementation */
int atoi(const std::string &str){
int num = 0;
for(std::string::iterator it = str.begin(); it != str.end(); ++it ){
if( *it < '0' || *it > '9'){
return num;
}
num = * 10;
num += *it - '0';
}
return num;
}