Oswin Jerome
Home
About
Blogs
Projects
#include
int main()
{
int a, b, t;
printf(" Enter value of A ? ");
scanf("%d", &a);
printf(" Enter value of B ? ");
scanf("%d", &b);
printf("\n Before swapping : A= %d, B= %d", a, b);
/****first method using third variable*/
t = a;
a = b;
b = t;
printf("\n After swapping (First method) : A= %d, B= %d\n", a, b);
/****second method without using third variable*/
a = a + b;
b = a - b;
a = a - b;
printf("\n After swapping (second method): A= %d, B= %d\n", a, b);
/****Third method using X-Or */
a ^= b;
b ^= a;
a ^= b;
printf("\n After swapping (third method) : A= %d, B= %d\n", a, b);
/****fourth method (single statement)*/
a = a + b - (b = a);
printf("\n After swapping (fourth method): A= %d, B= %d\n", a, b);
return 0;
}
#include
#include
using namespace std;
void swap(int arr[], int pos1, int pos2)
{
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int partition(int arr[], int low, int high, int pivot)
{
int i = low;
int j = low;
while (i <= high)
{
if (arr[i] > pivot)
{ i++;
}
else
{ swap(arr, i, j); i++; j++;
}
}
return j - 1;
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivot = arr[high];
int pos = partition(arr, low, high, pivot); quickSort(arr, low, pos - 1);
quickSort(arr, pos + 1, high);
}
}
int main()
{
int arr[] = {2, 5, 3, 7, 1, 9};
quickSort(arr, 0, 6 - 1);
cout << "The sorted array is: ";
for (int i = 0; i < 6; i++)
{
cout << arr[i] << " ";
}
}