Site icon Study Algorithms

Write a program to reverse an array.

Iterative way:
1) Initialize start and end indexes.
start = 0, end = n-1
2) In a loop, swap arr[start] with arr[end] and change start and end as follows.
start = start +1; end = end – 1

/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
	int temp;
	while(start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
}

/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
	int i;
	for (i=0; i < size; i++)
		printf("%d ", arr[i]);

	printf("\n");
}

/* Driver function to test above functions */
int main(void)
{
	int arr[] = {1, 2, 3, 4, 5, 6};
	printArray(arr, 6);
	rvereseArray(arr, 0, 5);
	printf("Reversed array is \n");
	printArray(arr, 6);
	return 0;
}

Time Complexity: O(n)

Recursive Way:
1) Initialize start and end indexes
start = 0, end = n-1
2) Swap arr[start] with arr[end]
3) Recursively call reverse for rest of the array.

/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
	int temp;
	if(start >= end)
		return;
	temp = arr[start];
	arr[start] = arr[end];
	arr[end] = temp;
	rvereseArray(arr, start+1, end-1);
}

/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
	int i;
	for (i=0; i < size; i++)
		printf("%d ", arr[i]);

	printf("\n");
}

/* Driver function to test above functions */
int main()
{
	int arr[] = {1, 2, 3, 4, 5};
	printArray(arr, 5);
	rvereseArray(arr, 0, 4);
	printf("Reversed array is \n");
	printArray(arr, 5);
	return 0;
}

Time Complexity: O(n)

Exit mobile version