Please help/ guide me for this one, i can somewhat do a but i keep deleting my c
ID: 3757144 • Letter: P
Question
Please help/ guide me for this one, i can somewhat do a but i keep deleting my code and redoing it to the point i am just getting confused with my thought process.
Problem Solving - In this problem set, include a single main file that contains functions that can be called from a main function. For this problem, you are allowed to use external sort functions and data structures from the STL or Java.util.etc Assume you have an array of size N containing integers. Your client asks you to order the array in terms of local maxima and local minima. Example: (a b cde...], a>b, bExplanation / Answer
//C++ program
#include <iostream>
using namespace std;
//Bubblesort uesd to sort arry in ascending order
void bubblesort(int a[],int n){
for(int i=n-1;i>=0;i--){
for(int j=0;j<i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int n;
cout<<"Enter size of array : ";
cin>>n;
int arr[n];
cout<<"Enter array ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
bubblesort(arr,n);
for (int i=0; i<n-1; i += 2)
swap(&arr[i], &arr[i+1]);
for(int i=0;i<n;i++){
cout<<arr[i];
if(i!=n-1)cout<<" ,";
}
return 0;
}
//complexity O(nlogn)//best case(using O(nlogn) sorting tecnique)
//it can't be linear because no algorithm to sort array in linear time.