Skip to content

Instantly share code, notes, and snippets.

@SanowerTamjit
Created March 11, 2019 15:10
Show Gist options
  • Save SanowerTamjit/3f74dd7573df3a3ea92f5f53b31fd486 to your computer and use it in GitHub Desktop.
Save SanowerTamjit/3f74dd7573df3a3ea92f5f53b31fd486 to your computer and use it in GitHub Desktop.
#include<stdio.h>
int length;
int list[1000];
void swapHalves(){
int tempList[length];
int mid = length/2;
for(int i = 0; i < length; i++)
tempList[i] = list[i];
if(length%2 == 1){ //If ODD
//Swap first half to last half
for(int i = 0; i < mid; i++)
list[mid+i+1] = tempList[i];
list[mid] = tempList[mid];
//Swap last half to first half
for(int i = 0; i < mid; i++)
list[i] = tempList[mid+i+1];
}
else{ // If Even
//Swap first half to last half
for(int i = 0; i < mid; i++)
list[mid+i] = tempList[i];
//Swap last half to first half
for(int i = 0; i < mid; i++)
list[i] = tempList[mid+i];
}
}
void swap(){
int tempList[length];
for(int i = 0; i < length; i++)
tempList[i] = list[i];
if(length%2 == 0)
for(int i = 0; i < length/2; i++)
list[i+length/2] = tempList[i];
else
for(int i = 0; i < length/2; i++)
list[i+(length/2)+1] = tempList[i];
for(int i = length/2; i < length; i++)
list[i-length/2] = tempList[i];
}
void prrintList(){
for(int i = 0; i<length; i++){
printf("%d ",list[i]);
}
}
int main(){
int c = 1;
length = 0;
for (int i = 0; i < 5; i++)
{
list[i] = c++;
length++;
}
prrintList();
printf("\n\n");
swap();//Apu's Try
prrintList();
printf("\n\n");
//Reseting Data Again
c = 1;
length = 0;
for (int i = 0; i < 5; i++)
{
list[i] = c++;
length++;
}
swapHalves();
prrintList();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment