Created
October 26, 2016 03:51
-
-
Save abrarShariar/44bf7ccfe209ba85d393650681b7cd48 to your computer and use it in GitHub Desktop.
activity selection lab algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| using namespace std; | |
| int* getActivitySelection(int*,int*,int*,int,int*); | |
| void doSwap(int*,int*,int*,int,int); | |
| int main(){ | |
| int total_job; | |
| cout<<"Total Jobs: "; | |
| cin>>total_job; | |
| //s, f array | |
| int s[total_job]; | |
| int f[total_job]; | |
| int indexArr[total_job]; | |
| //initialize index Array | |
| for(int i=0;i<total_job;i++){ | |
| indexArr[i] = i; | |
| } | |
| //take input start/finish time | |
| for(int i=0;i<total_job;i++){ | |
| cout<<"Start["<<i<<"]: "; | |
| cin>>s[i]; | |
| cout<<"Finish["<<i<<"]: "; | |
| cin>>f[i]; | |
| cout<<endl; | |
| } | |
| //sorting here | |
| bool isSort = false; | |
| while(!isSort){ | |
| isSort = true; | |
| for(int i=1;i<total_job;i++){ | |
| if(f[i]<f[i-1]){ | |
| isSort = false; | |
| //doSwap(s,f,indexArr,i-1,i); | |
| swap(f[i],f[i-1]); | |
| swap(s[i],s[i-1]); | |
| swap(indexArr[i],indexArr[i-1]); | |
| } | |
| } | |
| } | |
| int *resArray = new int[total_job]; | |
| int sz=0; | |
| resArray = getActivitySelection(s,f,indexArr,total_job,&sz); | |
| for(int i=0;i<=sz;i++){ | |
| cout<<resArray[i]<<endl; | |
| } | |
| } | |
| int* getActivitySelection(int* start,int* finish,int* indexArr,int total_job,int* sz){ | |
| int* A = new int[total_job]; | |
| A[0] = indexArr[0]; | |
| int i = 0; | |
| int index = 1; | |
| for(int m=1;m<total_job;m++){ | |
| if(start[m] >= finish[i]){ | |
| A[index] = indexArr[m]; | |
| index++; | |
| } | |
| i = m; | |
| } | |
| *sz = index - 1; | |
| return A; | |
| } | |
| void doSwap(int* start,int* finish,int* indexArr,int index1,int index2){ | |
| //swap finish | |
| int temp = finish[index2]; | |
| finish[index2] = finish[index1]; | |
| finish[index1] = temp; | |
| //swap start | |
| temp = start[index2]; | |
| start[index2] = start[index1]; | |
| start[index1] = temp; | |
| //swap indexArr | |
| temp = indexArr[index2]; | |
| indexArr[index2] = indexArr[index1]; | |
| indexArr[index1] = indexArr[index2]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment