Today I will explain heap sort using stl. Sorting an array using heap or making an heap is very easy. Using STL makes it a cake walk. In the following code I have used these function from STL.
First is make_heap(). Simply passing the vector inside it makes the heap.
Second is pop_back() which pops the lowest valued or last element from the heap.
Third is pop_heap(). It rearranges the elements in the heap range [first,last) in such a way that the part considered a heap is shortened by...
Comma Separated Prime Numbers
This is the C++ program I used to generate the first 1 million prime numbers:
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
int main() {
const unsigned long long num = 1000000;
array<bool,num> a;
int n;
scanf("%d",&n);
a.fill(true);
a[0] = false;
a[1] = false;
int m = sqrt(num);
for(int i = 2; i<=m; i++){
if(a[i]){
for(int j = i*i; j<num; j+=i){
a[j] = false;
}
}
}
...
Insert at n'th positon, first position, last position, delete at n'th position, reverse, print, print using recursion, reverse print, etc the Linked List
#include <iostream>
#include <algorithm>
#include <stdio.h>
struct node{
int data;
struct node* link;
};
// This is the head pointer
struct node * head;
// This function inserts element at the first position of linked list
void insert_first(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = head;
head = temp;
}
// This...