week_3

week_3
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
3
Indexable
Never
#include <stdio.h>


void swap(int*x, int* y);
void sort(int* arr, int len);
void bubblesort(int* arr, int len);
int findMin(int* arr, int len, int index);
void printArr(int* arr, int len);

int main(int argc, char const *argv[])
{
	int arr[] = {3, 7, 2, 5, 9};
	int len = sizeof(arr) / sizeof(arr[0]); 
	sort(arr, len);
	for(int x = 0; x < len; x++)
	{
		printf("\n%d", arr[x]);
	}



	//findMin()'li sorting
	int arr1[] = {2, 1, 3, 5, 9,3,12,31,-5, 12, -3, 1, 0,-12,3};
	int len1 = sizeof(arr1) / sizeof(arr1[0]);

	printf("\nMin:%d\n\n", findMin(arr1, len1, 1));

	for(int i = 0; i < len1; i++)
	{
		swap(&arr1[findMin(arr1, len1, i)], &arr1[i]);
	}
	printArr(arr1, len1);

	//----------------------------------------------------------

	return 0;
}
void swap(int*x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;

}
void sort(int* arr, int len)
{
	for(int i = 0; i < len; i++)
	{
		for(int j = i; j < len ; j++)
		{
			if(arr[i] > arr[j])
			{
				swap(&arr[i], &arr[j]);
			}
		}
	}
}
int findMin(int* arr, int len, int index)
{
	int min = index;
	for(int i = index; i < len; i++)
	{
		if(arr[min] > arr[i])
			{
				min = i;
			}
	}

	return min;
}
void printArr(int* arr, int len)
{
	for(int x = 0; x < len; x++)
	{
		printf("arr[%2d]: %4d\n",x, arr[x]);
	}
}