-
Notifications
You must be signed in to change notification settings - Fork 47
/
HillClimbing.cpp
54 lines (52 loc) · 1.32 KB
/
HillClimbing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//hill climbing is a mathematical optimization technique which belongs to the family of local search.
//It is an iterative algorithm that starts with an arbitrary solution to a problem, then attempts to find a better solution by making an incremental change to the solution.
//If the change produces a better solution, another incremental change is made to the new solution, and so on until no further improvements can be found.
#include<iostream>
#include<cstdio>
using namespace std;
//Calculate the cost to analyze
int calcCost(int arr[],int N)
{
int c=0;
for(int i=0;i<N;i++)
{
for(int j=i+1;j<N;j++) if(arr[j]<arr[i]) c++;
}
return c;
}
//Swap elements
void swap(int arr[],int i,int j)
{
int tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
int main()
{
int N;
cin>>N;
int arr[N];
for(int i=0;i<N;i++)
cin>>arr[i];
int bestCost=calcCost(arr,N),newCost,swaps=0;;
while(bestCost>0)
{
for(int i=0;i<N-1;i++)
{
swap(arr,i,i+1);
newCost=calcCost(arr,N);
if(bestCost>newCost)
{
cout<<"After swap: "<<++swaps<<endl;
for(int i=0;i<N;i++)
cout<<arr[i]<<endl;
bestCost=newCost;
}
else swap(arr,i,i+1);
}
}
cout<<"Final Ans"<<endl;
for(int i=0;i<N;i++)
cout<<arr[i]<<endl;
return 0;
}