Test interview series --- Sorting Algorithm album --- Bubble Sorting --- incorrect answer correction, algorithm ---

Source: Internet
Author: User

Test interview series --- Sorting Algorithm album --- Bubble Sorting --- incorrect answer correction, algorithm ---
Original answer:

# Include <stdio. h> # define LEN 10 // ARRAY length void main (void) {int ARRAY [10] = {0, 6, 3, 2, 7, 5, 4, 9, 1, 8}; // printf ("\ n"); for (int a = 0; a <LEN; a ++) // print the ARRAY content {printf ("% d", ARRAY [a]) ;}int I = 0; int j = 0; bool isChange; // set the exchange flag for (I = 1; I <LEN; I ++) {// do the most LEN-1 sort isChange = 0; // before the start of this sort, the exchange flag should be false for (j = LEN-1; j> = I; j --) // for the current unordered partition ARRAY [I .. LEN] Bottom-up scan {if (ARRAY [j + 1] <ARRAY [j]) { // Exchange Record ARRAY [0] = ARRAY [j + 1]; // ARRAY [0] is not a sentinel, only the temporary storage unit ARRAY [j + 1] = ARRAY [j]; ARRAY [j] = ARRAY [0]; isChange = 1; // exchange occurred, therefore, set the switch flag to true} printf ("\ n"); for (a = 0; a <LEN; a ++) // print the ARRAY content {printf ("% d", ARRAY [a]);} if (! IsChange) // This sorting has not been exchanged. Terminate the algorithm {break ;}} printf ("\ n"); return;} in advance ;}

Train of Thought Analysis: Compare the size of adjacent images one by one from the sequence header, and obtain the maximum or minimum number at the end of the sequence. Then compare the remaining n-1 numbers one by one according to the above method. Finally, the sorting is completed. Error Analysis: an error at for (I = 1; I <LEN-1; I ++) causes ARRAY [0] Not to be sorted. For (a = 0; a <LEN; a ++) does not return the life type of local variable.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>> Corrected answer:
# Include <stdio. h ># include <string >#include <iostream> using namespace std; # define LEN 10 int _ tmain (int argc, _ TCHAR * argv []) {int ARRAY [10] = {0, 6, 3, 2, 7, 5, 4, 9, 1, 8 }; // ARRAY to be sorted for (int a = 0; a <LEN; a ++) // print the ARRAY content {printf ("% d", ARRAY [a]);} int I = 0; int j = 0, temp = 0; bool isChange; // set the switch flag for (I = 0; I <LEN-1; I ++) {// maximum number of LEN-1 sort isChange = 0; // before the start of this sort, the exchange flag should be false for (j = LEN-1; j> = I; j --) // ARRAY [I .. LEN] Bottom-up scan {if (ARRAY [j] <ARRAY [J-1]) {// swap record temp = ARRAY [j]; // temp do temporary storage unit ARRAY [j] = ARRAY [J-1]; ARRAY [J-1] = temp; isChange = 1; // exchange occurred, therefore, set the switch flag to true} printf ("\ n"); for (int a = 0; a <LEN; a ++) // print the ARRAY content {printf ("% d", ARRAY [a]);} if (! IsChange) // This sorting has not been exchanged, early termination of the algorithm {break ;}} printf ("\ n"); return 0 ;}
Correction analysis: temp is used to replace ARRAY [0] for data transfer; Sorting starts from ARRAY [0. Output after correction:

Write the Bubble Sorting Algorithm Using the sub-Algorithm Using Pseudo code. The sub-algorithm is used to bubble the non-sorted sub-table.

# Include "stdio. h"
# Define N 10
Main ()
{Int a [N + 1];
Int I, j, t;
For (I = 1; I <= N; I ++)
Scanf ("% d", & a [I]);
Printf ("\ n ");
For (j = 1; j <N; j ++)
For (I = 1; I <N-j; I ++)
If (a [I]> a [I + 1])
{T = a [I];
A [I] = a [I + 1];
A [I + 1] = t;
}
Printf ("the sorted numbers: \ n ");
For (I = 1; I <= N; I ++)
Printf ("% 4d", a [I]);
}

Sort algorithms use (improved) bubble sort Algorithms

# I nclude <stdlib. h>
# I nclude <time. h>

Void maopao (int source [], int n)
{
Int start = 0, end = n-1;
Int I;
While (start <= end)/* if there are other elements that are not determined */
{
For (I = start; I <end; I ++)/* Find the maximum number of remaining elements */
If (source [I]> source [I + 1])
{
Int t;
T = source [I];
Source [I] = source [I + 1];
Source [I + 1] = t;
}
End --;/* Find the maximum number */
For (I = end; I> start; I --)/* Find the minimum element of the remaining element */
If (source [I] <source [I-1])
{
Int t;
T = source [I];
Source [I] = source [I-1];
Source [I-1] = t;
}
Start ++;/* Find a minimum number */
}
}

Void output (int data [], int n)
{
Int I;
For (I = 0; I <n; I ++)
{
If (I % 10 = 0)
Printf ("\ n ");
Printf ("% 4d", data [I]);
}
}

Int check (int data [], int n)
{/* Check whether the result data is sorted in ascending order */
Int I;
For (I = 0; I <n-1; I ++)
If (data [I]> data [I + 1])
Return 0;
Return 1;
}

Void main ()
{
Int data [500];
Int I;
Srand (time (NULL ));
For (I = 0; I <500; I ++)
Data [I] = random (500 );
Printf ("\ nThe original data is: \ n ");
Output (data, 500 );
Maopao (data, 500 );
Printf ("\ nAfter sort: \ n ");
Output (data, 500 );
Printf ("\ n ");
If (check (data, 500) = 1)
Printf ("\ nRight .");
Else
Printf ("\ nWrong .");
}... Remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.