i want this code in c if lena 2 1 odd lenth mid lenarr 2

i want this code in c++

if len(a) % 2 == 1:
       #odd lenth
       mid = len(arr) /2
       count = 1
       while mid - count >= 0:
           left = mid -count
           right = mid + count

           if arr[left] >= arr[right]:
               arr[left], arr[right] = arr[right] , arr[left]
          
           ind = left +1
           while arr[ind] <= arr[left] :
               arr[left] , arr[ind] = arr[ind] , arr[left]
               left +=1
               ind +=1          

           ind =right -1
           while ind >=0 and arr[ind] >= arr[right] :
               arr[right] , arr[ind] = arr[ind] , arr[right]
               right -=1;
               ind -=1
           count +=1

   else:
       mid = len(arr) /2
       left = mid-1
       right = mid
       while left >= 0 and right <= len(arr) -1:
          
           if arr[left] > arr[right]:
               arr[left], arr[right] = arr[right] , arr[left]
          
           ind = left +1
           while arr[ind] < arr[left] :
               arr[left] , arr[ind] = arr[ind] , arr[left]
               left +=1
               ind +=1          

           ind =right -1
           while arr[ind] > arr[right] :
               arr[right] , arr[ind] = arr[ind] , arr[right]
               right -=1;
               ind -=1

           left -=1
           right +=1


   print arr

arr = [-8,4 ,-8 , 3, 2 , 2 , -8 , - 6 , 56 , 43, -8777]

double_insertion_sort(arr)

arr = [-8,4 ,-8 , 3, 2 , 2 , -8 , - 6 , 56 , 43, -8777, -9999999 ]

double_insertion_sort(arr)

Solution

Here I am writing a simple user defined funciton for insertion sorting


int double_insertion_sort(a){
   int i, j, k, temp;
for (i = 1; i < a.length(); i++){
for (j = i; j >= 1; j--) {
if (a[j] < a[j-1]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
else
break;
}
}
return a;
}

i want this code in c++ if len(a) % 2 == 1: #odd lenth mid = len(arr) /2 count = 1 while mid - count >= 0: left = mid -count right = mid + count if arr[left]
i want this code in c++ if len(a) % 2 == 1: #odd lenth mid = len(arr) /2 count = 1 while mid - count >= 0: left = mid -count right = mid + count if arr[left]

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site