Code optimization for insertion sort advance analysis - c++

#include <string>
#include <numeric>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
int n, n1, n2, index, max = 0;
long cnt = 0;
vector <int> arr;
vector <int> arr1;
cin >> n;
for (int i = 0; i < n; i++) {
cnt = 0, max = 0;
cin >> n1;
arr.clear();
arr1.clear();
for (int j = 0; j < n1; j++) {
cin >> n2;
arr.push_back(n2);
if (n2 > max) max = n2;
}
for (int j = 0; j <= max; j++) {
arr1.push_back(j);
arr1[j] = 0;
}
for (int j = n1 - 1; j >= 0; j--) {
index = arr[j] - 1;
while (index > 0) {
cnt += arr1[index];
index -= (index & -index);
}
index = arr[j];
while (index <= max) {
arr1[index] += 1;
index += (index & -index);
}
}
cout << cnt << endl;
}
}
I've been trying to solve the insertion sort advanced analysis but my code times out. Is there a way I can optimize the code more? The objective of the problem is to find the total number of times the array has to shift.
I've tried to use BIT for the first time.
n is for the number of tests.
n1 is for the amount of numbers that will be inputted.
arr is a vector to save the inputs.
arr1 is a vector to save the amount for each number.
Sorry for the bad english.

Related

I was trying to run half nested loops and try doing kind of a half sorting of an array but it doesn't seem to work can you tell me why its not working

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, a[n];
cin >> n;
float b = n / 2;
for (int i = 1; i <= n; i++)
cin >> a[i];
// the code seems to be not working from here till the point marked below
if (n % 2 == 0)
{
for (int k = 1; k <= b; k++)
{
for (int j = (b + 1); j <= n; j++)
{
if (a[k] > a[j])
swap(a[k], a[j]);
}
}
}
// till this point(pls tell me what's wrong with this part of the code)
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
getch();
return 0;
}

Non-repeating random numbers in vector C++

I'm trying to store random numbers in vector, but I want each number to be unique. Can I do that with for loop without using unique() or random_shuffle() ?
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
vector<int> v;
for (unsigned int i = 0; i < 30; i++) {
v.push_back(rand() % 30);
}
for (unsigned int j = 0; j < 30; j++) {
cout << v[j] << endl;
}
return 0;
}
The classic Fisher–Yates shuffle can also be used to generate a shuffled vector directly, in one pass
vector<unsigned> v;
for (unsigned i = 0; i < 30; ++i)
{
unsigned j = rand() % (i + 1);
if (j < i)
{
v.push_back(v[j]);
v[j] = i;
}
else
v.push_back(i);
}
You should probably generate vector and just shuffle it:
#include <iostream>
#include <ctime>
#include <utility>
int main()
{
std::srand(static_cast<unsigned int>(std::time(NULL)));
size_t const n = 30;
std::vector<int> v(n);
//make vector
for (size_t i = 0; i < n; ++i)
v[i] = static_cast<int>(i);
//shuffle
for (size_t i = n - 1; i > 0; --i)
std::swap(v[i], v[static_cast<size_t>(rand()) % (i + 1)]);
//print
for (size_t i = 0; i < n; ++i)
std::cout << (i > 0 ? "," : "") << v[i];
return 0;
}
Prints, for example:
27,24,2,23,13,6,9,14,11,5,15,18,16,29,22,12,26,20,10,8,28,25,7,4,1,17,0,3,19,21

vector array - sorting failure

I have problem with sorting an array. I don't know why my codes does not sort an array properly. I am new in programming so be gentle on me. Here's a code. Also other functions like merge or quick sort does not work too. Thanks in advance for answer.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
std::vector<int> bubbleSort(std::vector<int>& Array)
{
for (unsigned int j = 1; j < Array.size() - 1; ++j)
{
for (unsigned int i = 0; i < Array.size() - 1; i++)
{
if (Array[i] > Array[++i])
{
std::swap(Array[i], Array[++i]);
}
}
}
return Array;
}
int main()
{
for (int i = 0; i < 10;i++)
{
int N; //array size
srand(std::time(NULL));
std::cout << " array size: ";
std::cin >> N;
std::vector <int> Array;
//fill array
for (int i = 1; i <= N; i++)
Array.push_back(i);
for (int i = N - 1; i > 0; i--)
{
int j = rand() % i;
std::swap(Array[i], Array[j]);
}
bubbleSort(Array);
for (unsigned int i = 0; i < Array.size(); i++)
{
std::cout << Array.at(i) << std::endl;
}
}
system("pause");
}
It looks like you're unintentionally incrementing i within the loop. Don't use ++i, use i+1 instead. Also change your loop termination condition to just i < Array.size() instead of i < Array.size() - 1

Can you help me in finding the best algorithm? [duplicate]

The subarray contains both positive and negative numbers. You have to find a maximum sum subarray such that the length of the sub-array is greater than or equal to k.
Here is my code in c++ using Kadane's algorithm.
#include <iostream>
using namespace std;
int main(){
int n,k;
cin >> n >> k;
int array[n];
int sum = 0;
int maxsum = 0;
int beststarts[n];
for(int i = 0;i < n; i++){
cin >> array[i];
}
for(int i = 0;i < k-1;i ++){
sum = sum+array[i];
beststarts[i] = 0;
}
for(int i = k-1;i < n; i++){ //best end search with min length;
sum = sum+array[i];
int testsum = sum;
if(i > 0){
beststarts[i] = beststarts[i-1];
}
for(int j = beststarts[i] ;i-j > k-1;j ++){
testsum = testsum - array[j];
if(testsum > sum){
beststarts[i] = j+1;
sum = testsum;
}
}
if(sum > maxsum){
maxsum = sum;
}
}
cout << maxsum;
return 0;
}
My code is working fine but it is very slow, and i cant think of any ways to improve my code. I have also read this question Find longest subarray whose sum divisible by K but that is not what i want, the length can be greater than k also.
Solution based on this answer
Live demo
#include <algorithm>
#include <iterator>
#include <iostream>
#include <numeric>
#include <ostream>
#include <utility>
#include <vector>
// __________________________________________________
template<typename RandomAccessIterator> typename std::iterator_traits<RandomAccessIterator>::value_type
max_subarr_k(RandomAccessIterator first,RandomAccessIterator last,int k)
{
using namespace std;
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
if(distance(first,last) < k)
return value_type(0);
RandomAccessIterator tail=first;
first+=k;
value_type window=accumulate(tail,first,value_type(0));
value_type max_sum=window, current_sum=window;
while(first!=last)
{
window += (*first)-(*tail) ;
current_sum = max( current_sum+(*first), window );
max_sum = max(max_sum,current_sum);
++first;
++tail;
}
return max_sum;
}
// __________________________________________________
template<typename E,int N>
E *end(E (&arr)[N])
{
return arr+N;
}
int main()
{
using namespace std;
int arr[]={1,2,4,-5,-4,-3,2,1,5,6,-20,1,1,1,1,1};
cout << max_subarr_k(arr,end(arr),4) << endl;
cout << max_subarr_k(arr,end(arr),5) << endl;
}
Output is:
14
11
int w(0);
for (int i=0; i < k; i++) w += a[i];
int run_sum(w), max_sum(w);
for (int i=k; i < n; i++) {
w = a[i] + max(w, w-a[i-k]); // window will such that it will include run_sum
run_sum = max(run_sum + a[i], w);
max_sum = max(run_sum, max_sum);
}
return max_sum;

How to find a subarray with minimum k length and maximum sum?

The subarray contains both positive and negative numbers. You have to find a maximum sum subarray such that the length of the sub-array is greater than or equal to k.
Here is my code in c++ using Kadane's algorithm.
#include <iostream>
using namespace std;
int main(){
int n,k;
cin >> n >> k;
int array[n];
int sum = 0;
int maxsum = 0;
int beststarts[n];
for(int i = 0;i < n; i++){
cin >> array[i];
}
for(int i = 0;i < k-1;i ++){
sum = sum+array[i];
beststarts[i] = 0;
}
for(int i = k-1;i < n; i++){ //best end search with min length;
sum = sum+array[i];
int testsum = sum;
if(i > 0){
beststarts[i] = beststarts[i-1];
}
for(int j = beststarts[i] ;i-j > k-1;j ++){
testsum = testsum - array[j];
if(testsum > sum){
beststarts[i] = j+1;
sum = testsum;
}
}
if(sum > maxsum){
maxsum = sum;
}
}
cout << maxsum;
return 0;
}
My code is working fine but it is very slow, and i cant think of any ways to improve my code. I have also read this question Find longest subarray whose sum divisible by K but that is not what i want, the length can be greater than k also.
Solution based on this answer
Live demo
#include <algorithm>
#include <iterator>
#include <iostream>
#include <numeric>
#include <ostream>
#include <utility>
#include <vector>
// __________________________________________________
template<typename RandomAccessIterator> typename std::iterator_traits<RandomAccessIterator>::value_type
max_subarr_k(RandomAccessIterator first,RandomAccessIterator last,int k)
{
using namespace std;
typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
if(distance(first,last) < k)
return value_type(0);
RandomAccessIterator tail=first;
first+=k;
value_type window=accumulate(tail,first,value_type(0));
value_type max_sum=window, current_sum=window;
while(first!=last)
{
window += (*first)-(*tail) ;
current_sum = max( current_sum+(*first), window );
max_sum = max(max_sum,current_sum);
++first;
++tail;
}
return max_sum;
}
// __________________________________________________
template<typename E,int N>
E *end(E (&arr)[N])
{
return arr+N;
}
int main()
{
using namespace std;
int arr[]={1,2,4,-5,-4,-3,2,1,5,6,-20,1,1,1,1,1};
cout << max_subarr_k(arr,end(arr),4) << endl;
cout << max_subarr_k(arr,end(arr),5) << endl;
}
Output is:
14
11
int w(0);
for (int i=0; i < k; i++) w += a[i];
int run_sum(w), max_sum(w);
for (int i=k; i < n; i++) {
w = a[i] + max(w, w-a[i-k]); // window will such that it will include run_sum
run_sum = max(run_sum + a[i], w);
max_sum = max(run_sum, max_sum);
}
return max_sum;