Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
inversion of array using merge sort
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
long long merge(long long arr[],long long lo,long long mid,long long hi,long long n,long long &c) { long long temp[n]; long long i=lo; long long j=mid+1; long long k=lo; while(i<=mid && j<=hi) { if(arr[i]<=arr[j]) { temp[k]=arr[i]; i++,k++; } else if(arr[j]<arr[i]) { temp[k]=arr[j]; j++,k++; c+=(mid-i+1); } } if(i>mid) { while(j<=hi) { temp[k]=arr[j]; j++;k++; } } else { while(i<=mid) { temp[k]=arr[i]; i++,k++; } } for(int q=lo;q<=hi;q++) { arr[q]=temp[q]; } // for(int i=0;i<n;i++) // cout<<arr[i]<< " "; // cout<<endl; return c; } long long mergesort(long long arr[],long long lo,long long hi,long long n,long long &c) { if(lo<hi) { long long mid=lo+(hi-lo)/2; mergesort(arr,lo,mid,n,c); mergesort(arr,mid+1,hi,n,c); merge(arr,lo,mid,hi,n,c); return c; } } long long int inversionCount(long long arr[], long long n) { // Your Code Here long long c=0; return mergesort(arr,0,n-1,n,c); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion