Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 81 additions & 67 deletions Sorting/mergeSort.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,86 @@
class Sort
{
void merge(int arr[], int left, int middle, int right)
{
int low = middle - left + 1;
int high = right - middle;

int L[] = new int[low];
int R[] = new int[high];

public class MergeSort {

public static void main(String args[]) {
int arr[] = {23, 12, 85, 39, 98, 7, 16, 97, 54};

System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array");
printArray(arr);
}

void merge(int arr[], int low, int mid, int up) {
int n1 = mid - low + 1;
int n2 = up - mid;

int Left[] = new int[n1];
int Right[] = new int[n2];

for (int i = 0; i < n1; ++i)
Left[i] = arr[low + i];
for (int j = 0; j < n2; ++j)
Right[j] = arr[mid + 1 + j];

int i = 0, j = 0;

int k = low;
while (i < n1 && j < n2) {
if (Left[i] <= Right[j]) {
arr[k] = Left[i];
i++;
}
else {
arr[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = Left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = Right[j];
j++;
k++;
}
}
int i = 0, j = 0;

for (i = 0; i < low; i++)
{
L[i] = arr[left + i];
}
for (j = 0; j < high; j++)
{
R[j] = arr[middle + 1 + j];
}


int k = left;
i = 0;
j = 0;

void sort(int arr[], int low, int up) {
if (low < up) {
int mid =low + (up-low)/2;
while (i < low && j < high)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

while (i < low)
{
arr[k] = L[i];
i++;
k++;
}

while (j < high)
{
arr[k] = R[j];
j++;
k++;
}
}


sort(arr, low, mid);
sort(arr, mid + 1, up);
void mergeSort(int arr[], int left, int right)
{
int middle;
if (left < right) {
middle = (left + right) / 2;

mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);

merge(arr, left, middle, right);
}
}

void display(int arr[])
{
for (int i=0; i<arr.length; ++i)
{
System.out.print(arr[i]+" ");
}
}

merge(arr, low, mid, up);
}
}

static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 9, 3, 1, 5, 13, 12 };
Sort ob = new Sort();
ob.mergeSort(arr, 0, arr.length - 1);
ob.display(arr);
}
}
71 changes: 71 additions & 0 deletions jobSequencingProbem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// You are given a set of N jobs where each job comes with a deadline and profit.
// The profit can only be earned upon completing the job within its deadline.
// Find the number of jobs done and the maximum profit that can be obtained.
// Each job takes a single unit of time and only one job can be performed at a time.

// Basic Outline of the approach:-

// Sort the jobs in descending order of profit.
// If the maximum deadline is x, make an array of size x .Each array index is set to -1 initially as no jobs have been performed yet.
// For every job check if it can be performed on its last day.
// If possible mark that index with the job id and add the profit to our answer.
// If not possible, loop through the previous indexes until an empty slot is found.

// Time Complexity: O(N log N) + O(N*M).
// Space Complexity: O(M)

#include<bits/stdc++.h>

using namespace std;
// A structure to represent a job
struct Job {
int id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};
class Solution {
public:
bool static comparison(Job a, Job b) {
return (a.profit > b.profit);
}
//Function to find the maximum profit and the number of jobs done
pair < int, int > JobScheduling(Job arr[], int n) {

sort(arr, arr + n, comparison);
int maxi = arr[0].dead;
for (int i = 1; i < n; i++) {
maxi = max(maxi, arr[i].dead);
}

int slot[maxi + 1];

for (int i = 0; i <= maxi; i++)
slot[i] = -1;

int countJobs = 0, jobProfit = 0;

for (int i = 0; i < n; i++) {
for (int j = arr[i].dead; j > 0; j--) {
if (slot[j] == -1) {
slot[j] = i;
countJobs++;
jobProfit += arr[i].profit;
break;
}
}
}

return make_pair(countJobs, jobProfit);
}
};
int main() {
int n = 4;
Job arr[n] = {{1,4,20},{2,1,10},{3,2,40},{4,2,30}};

Solution ob;
//function call
pair < int, int > ans = ob.JobScheduling(arr, n);
cout << ans.first << " " << ans.second << endl;

return 0;
}