PROGRAMMING LANGUAGE MUST BE IN C. PLEASE USE BUBBLE SORT FOR SORTING. PLEASE EX
ID: 3795198 • Letter: P
Question
PROGRAMMING LANGUAGE MUST BE IN C. PLEASE USE BUBBLE SORT FOR SORTING. PLEASE EXPLAIN WHAT YOUR CODE DOES.
DATA DISPLAY 10 NUMBERS PER LINE.
ARRAY INPUT:
Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make a copy of one array into another, and then to sort the second array. This would allow us to view the data both in the original order and in the sorted order This might be fine in many cases, but if the data set is large and memory limited (say, perhaps, in an embedded system), this solution might not be practicable. A more elegant solution is to sort the array indirectly, i.e., by using pointers. With this technique we wouldn't change the positions of the actual data items in the array; we would only change the values of the pointers that point into the array. Nevertheless, when this type of sorting is performed, we still will be able to access the data both in its original order, by using the original array, but also in sorted order, by using the array of pointers Here are some diagrams that illustrate the ideaExplanation / Answer
#include<stdio.h>
void swap(int* a,int* b)//to swap values in array
{
int t;
t =*a;
*a = *b;
*b = t;
}
void sort(int* array, int count)//Buuble sort
{
int i, j;
for(i = 0; i < count-1; ++i){
for(j=0; j<count-1-i; ++j){
if(*(array+j) >*(array+j+1)){
swap((array+j),(array+(j+1)));
}
}
}
}
void DisplayDataArray(int array[], int count)//function to display data array
{
int c=0;
int i;
for ( i = 0; i < count; ++i)
{
if(c==10)
{
c=0;
printf(" "); }
else
{
printf("%d ",array[i]);
c++;
}
}
}
void DisplayDPointerArray(int* array, int count)//function to display pointer array
{
int c=0;int i;
for ( i = 0; i < count; ++i)
{
if(c==10)
{
c=0;
printf(" ");
}
else
{
printf("%d ",*array);
array++;
c++;
}
}
}
int main(int argc, char const *argv[])
{
int dataArray[150]={71, 1899, 272, 1694, 1697, 296, 722, 12, 2726, 1899,
1374, 1541, 1923, 1904, 1083, 1462, 2981, 1929, 304, 2550,
1059, 1860, 1963, 516, 647, 1607, 590, 157, 2351, 753,
2455, 349, 79, 1634, 368, 1992, 2401, 357, 1478, 1601,
239, 365, 2453, 2283, 2432, 1223, 2739, 2487, 2714, 1391,
1972, 2805, 1504, 413, 1647, 2750, 44, 64, 934, 1008,
1429, 1427, 315, 2499, 1620, 1816, 2441, 2557, 2188, 531,
1514, 2825, 449, 265, 2064, 1022, 34, 1864, 1861, 1516,
1465, 2327, 398, 2769, 563, 194, 429, 942, 1795, 223,
2406, 780, 780, 61, 133, 195, 495, 1774, 1934, 2171,
433, 1417, 292, 324, 2929, 1597, 1470, 764, 593, 891,
679, 47, 1778, 2532, 1862, 2636, 549, 2923, 2270, 1101,
1607, 2395, 726, 1111, 892, 1988, 555, 379, 224, 298,
1660, 2203, 2385, 2159, 2574, 705, 2513, 1755, 313, 173,
148, 2449, 259, 1006, 1221, 2259, 2020, 1484, 2717, 2400};//initialise data array
int *pointerArray[150];
int count=150;
int i;
for ( i = 0; i < count; ++i)//initialise pointer array
{
pointerArray[i]=&dataArray[i];
}
printf("Unsorted Data array is ");
DisplayDataArray(dataArray,count);
sort(dataArray,count);
printf(" Sorted Data array is ");
DisplayDataArray(dataArray,count);
sort(*pointerArray,count);
printf(" Sorted Pointer array is ");
DisplayDataArray(*pointerArray,count);
return 0;
}
============================================================
akshay@akshay-Inspiron-3537:~/Chegg$ gcc bubblesort.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Unsorted Data array is
71 1899 272 1694 1697 296 722 12 2726 1899
1541 1923 1904 1083 1462 2981 1929 304 2550 1059
1963 516 647 1607 590 157 2351 753 2455 349
1634 368 1992 2401 357 1478 1601 239 365 2453
2432 1223 2739 2487 2714 1391 1972 2805 1504 413
2750 44 64 934 1008 1429 1427 315 2499 1620
2441 2557 2188 531 1514 2825 449 265 2064 1022
1864 1861 1516 1465 2327 398 2769 563 194 429
1795 223 2406 780 780 61 133 195 495 1774
2171 433 1417 292 324 2929 1597 1470 764 593
679 47 1778 2532 1862 2636 549 2923 2270 1101
2395 726 1111 892 1988 555 379 224 298 1660
2385 2159 2574 705 2513 1755 313 173 148 2449
1006 1221 2259 2020 1484 2717 2400
Sorted Data array is
12 34 44 47 61 64 71 79 133 148
173 194 195 223 224 239 259 265 272 292
298 304 313 315 324 349 357 365 368 379
413 429 433 449 495 516 531 549 555 563
593 647 679 705 722 726 753 764 780 780
892 934 942 1006 1008 1022 1059 1083 1101 1111
1223 1374 1391 1417 1427 1429 1462 1465 1470 1478
1504 1514 1516 1541 1597 1601 1607 1607 1620 1634
1660 1694 1697 1755 1774 1778 1795 1816 1860 1861
1864 1899 1899 1904 1923 1929 1934 1963 1972 1988
2020 2064 2159 2171 2188 2203 2259 2270 2283 2327
2385 2395 2400 2401 2406 2432 2441 2449 2453 2455
2499 2513 2532 2550 2557 2574 2636 2714 2717 2726
2750 2769 2805 2825 2923 2929 2981
Sorted Pointer array is
12 34 44 47 61 64 71 79 133 148
173 194 195 223 224 239 259 265 272 292
298 304 313 315 324 349 357 365 368 379
413 429 433 449 495 516 531 549 555 563
593 647 679 705 722 726 753 764 780 780
892 934 942 1006 1008 1022 1059 1083 1101 1111
1223 1374 1391 1417 1427 1429 1462 1465 1470 1478
1504 1514 1516 1541 1597 1601 1607 1607 1620 1634
1660 1694 1697 1755 1774 1778 1795 1816 1860 1861
1864 1899 1899 1904 1923 1929 1934 1963 1972 1988
2020 2064 2159 2171 2188 2203 2259 2270 2283 2327
2385 2395 2400 2401 2406 2432 2441 2449 2453 2455
2499 2513 2532 2550 2557 2574 2636 2714 2717 2726
2750 2769 2805 2825 2923 2929 2981
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.