Escolar Documentos
Profissional Documentos
Cultura Documentos
#include <iostream>
if(lower_lim == upper_lim)
return array[lower_lim];
return order <= x ? randomSelect(array, lower_lim, x, order) : randomSelect(array, x+1, upper_lim, order
- x);
swap(&array[pivot], &array[lower_lim]);
swap(&array[i++], &array[j]);
swap(&array[lower_lim], &array[i-1]);
return i-1;
*i = *j;
*j = temp;
int main() {
Output
3rd Order Statistic: 3
Radix Sort using Counting Sort
#include <iostream>
max = items[i];
return max;
count[(items[i]/exponent) % 10] += 1;
count[i] += count[i-1];
if(count[index] > 0) {
output[count[index]-1] = items[i];
count[index] -= 1;
}
for(i = 0; i < numItems; i++)
items[i] = output[i];
int main() {
sort(a, 3);
return 0;
Output
1 10 22 36 498
Bucket Sort
#include<iostream>
#include<vector>
#include<algorithm>
vector<float> bucket[size];
bucket[int(size*array[i])].push_back(array[i]);
int index = 0;
while(!bucket[i].empty()) {
array[index++] = *(bucket[i].begin());
bucket[i].erase(bucket[i].begin());
int main() {
int n;
cin >> n;
display(arr, n);
Output
Enter the number of elements: 5
Enter elements:
#include <cstring>
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
else
int main() {
cout << "Longest Common Subsequence: " << lcs(x, y, m, n) << endl;
return 0;
Output
Longest Common Subsequence: 4
Knapsack Problem
#include <bits/stdc++.h>
// value of Item
struct Item
// Constructor
{}
};
/*
cout << arr[i].value << " " << arr[i].weight << " : "
<< ((double)arr[i].value / arr[i].weight) << endl;
*/
curWeight += arr[i].weight;
finalvalue += arr[i].value;
else
break;
return finalvalue;
int main()
return 0;
Output
Maximum value we can obtain = 240
BFS and DFS in Graph
#include<iostream>
#include <list>
class Graph
// lists
list<int> *adj;
public:
};
Graph::Graph(int V)
this->V = V;
void Graph::BFS(int s)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
// vertices of a vertex
list<int>::iterator i;
while(!queue.empty())
s = queue.front();
queue.pop_front();
if (!visited[*i])
visited[*i] = true;
queue.push_back(*i);
// print it
visited[v] = true;
// to this vertex
list<int>::iterator i;
if (!visited[*i])
DFSUtil(*i, visited);
visited[i] = false;
DFSUtil(v, visited);
int main()
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.BFS(2);
cout << "Following is Depth First Traversal"
g.DFS(2);
return 0;
Output
Following is Breadth First Traversal (starting from vertex 2)
2031
2013
#define V 4
// Solves the all-pairs shortest path problem using Floyd Warshall algorithm
/* dist[][] will be the output matrix that will finally have the shortest
int dist[V][V], i, j, k;
dist[i][j] = graph[i][j];
pairs of vertices such that the shortest distances consider only the
----> After the end of an iteration, vertex no. k is added to the set of
printSolution(dist);
}
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("\n");
int main() {
{INF, 0, 3, INF},
};
floydWarshall(graph);
return 0;
Output
The following matrix shows the shortest distances between every pair of vertices
0 5 8 9
INF 0 3 4
INF INF 0 1
struct Edge
};
// weighted graph
struct Graph
int V, E;
};
graph->V = V;
graph->E = E;
return graph;
}
// A utility function used to print the solution
int V = graph->V;
int E = graph->E;
int dist[V];
// as INFINITE
dist[i] = INT_MAX;
dist[src] = 0;
// path from src to any other vertex can have at-most |V| - 1
// edges
{
for (int j = 0; j < E; j++)
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
// is a cycle.
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
printArr(dist, V);
return;
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = -1;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;
graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;
graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;
BellmanFord(graph, 0);
return 0;
Output
Vertex Distance from Source
0 0
1 -1
2 2
3 -2
4 1