View Code of Problem 49

#include<stdio.h>
#include<stdlib.h>
void swap(int* a, int* b){
  int temp = *a;
  *a = *b;
  *b = temp;
}
int main(){
  int n,temp;
  scanf("%d", &n);
  int* array = (int*)malloc(sizeof(int)*n);
  for(int i =0;i<n;++i){
    scanf("%d", array+i);
  }
  for(int i =1;i<n-1;++i){
    if(array[i] < array[0])
      swap(array+i, array);
    else if(array[i] > array[n-1])
      swap(array+i, array+n-1);
  }
  for(int i = 0;i<n;++i){
    printf("%d ", array[i]);
  }
  printf("%d", array[n-1]);
  free(array);
  return 0;
}

Double click to view unformatted code.


Back to problem 49