Bubbling thought taken, select two first, then set to one cycle, but not the same because the data type table adjacency table header node and edge node table, so this is not equivalent to a head of the linked list node sort of.
Can optimize it is: If you take the exchange did not happen again, explained that it had ordered, and therefore will not need to be bubbling up. Directly out of the loop, reducing cycle times.
In the table edge node I is stored in string type data, according to their index in the header node sorted in descending order, it is easy to get the same result for the same process in FIG bfs and dfs in, and user input does not vary so that different results.
map <string, int> mmp; // mapping between the strings and the coordinates of the struct ArcNode // edge list node { String name; int wigth; ArcNode * Next; }; struct // VertexNode vertex node list { String name; // name ArcNode * firstarc; // points to the first element of a table edge node };
Graph class { Private: int pointnum, eagenum; VertexNode Point [MAXN]; // vertex array ArcNode * last [maxn]; // points to the tail node int vis [maxn]; // tag array int dis [maxn]; // distance array void insertElement (int POS, ArcNode * P); void The sortlist (VertexNode nil); public: Graph (); // constructor void Create (pointnum int, int eagenum); void initvis (); void get_agree (); // output of void dfs (string begin, int flag ); // depth first search void bfs (string begin); // BFS void dfs_all (String the begin); void Sort (); void bfs_all (String the begin); int get_connected_num (); // find connected components bool is_connected (); // if communication bool is_exit (string str); // if there are little void delete_point (String STR); void the Dijkstra (String the begin); void Prim (); void the Kruskal (); // the TODO: Minimum spanning Tree, void Print (); };
void Graph::sortlist(VertexNode nil) { ArcNode *pre,*now; int cnt=0; pre=nil.firstarc; while(pre) { cnt++; pre=pre->next; } if(cnt<=1) { return ; } //printf("cnt=%d\n",cnt); ArcNode *tmp; int ok=1; for(int i=0;i<cnt;i++) { pre=nil.firstarc; now=nil.firstarc->next; ok=1; while(now) { if(mmp[now->name]>mmp[pre->name]) { swap(now->name,pre->name); swap(now->wigth,pre->wigth); ok=0; } pre=now; now=pre->next; } if(ok==1) { break; } } /*pre=nil.firstarc; while(pre) { cout<<pre->name<<"|"<<pre->wigth<<endl; pre=pre->next; }*/ }
void Graph::sort() { for(int i=0;i<pointnum;i++) sortlist(point[i]); }