Technical Discussion :: Data Structure
Home > Technical Interview Q&A > Data Structure > Discussion
29 / 72
Write a program to Sort the given unsorted list.
Answer:
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
Asked In ::
MM
20 Aug, 2021 4:42 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
MM
20 Aug, 2021 4:43 PM
#include
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
for (i = 0; i < n; i )
cout<< a[i] <<" ";
cout<
selectionSort(a, n);
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j )
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"Given array is:"<
cout<< a[i] <<" ";
cout<
printf("\nSorted array is: \n");
for (i = 0; i < n; i )
cout<< a[i] <<" ";
return 0;
}